Kunal Batra
Kunal Batra

Reputation: 991

Provided id of the wrong type for class classname Expected: class java.lang.Integer, got class java.lang.String

Here is my sql table description

desc product_details;
+--------------------+-------------+------+-----+---------+-------+
| Field              | Type        | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+-------+
| action             | varchar(5)  | YES  |     | NULL    |       |
| version            | varchar(2)  | YES  |     | NULL    |       |
| productCatalogName | varchar(50) | YES  |     | NULL    |       |
| productId          | varchar(25) | NO   |     | NULL    |       |
| price              | varchar(10) | NO   |     | NULL    |       |
| Operator_Code      | varchar(7)  | YES  |     | NULL    |       |
| product_name       | varchar(70) | YES  |     | NULL    |       |
| subs_id            | int(11)     | NO   | PRI | 0       |       |
+--------------------+-------------+------+-----+---------+-------+

Every time when i call the get method

ProductDetail productDetail = (ProductDetail )session.get(ProductDetail .class, subs_id);

i am getting exception:

nested exception is org.hibernate.TypeMismatchException: Provided id of the wrong type for class tpay.table.ProductDetail. Expected: class java.lang.Integer, got class java.lang.String




package tpay.table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="product_details")
public class ProductDetail {

    @Id
    @Column(name="subs_id")
     int subs_id;

    @Column(name="action")
    String action;

    @Column(name="version")
    String version;

    @Column(name="productCatalogName")
    String productCatalogName;

    @Column(name="productId")
    String productId;

    @Column(name="price")
    String price;

    @Column(name="Operator_Code")
    String Operator_Code;

    @Column(name="product_name")
    String product_name;



public String getAction() {
    return action;
}

public void setAction(String action) {
    this.action = action;
}

public String getVersion() {
    return version;
}

public void setVersion(String version) {
    this.version = version;
}

public String getProductCatalogName() {
    return productCatalogName;
}

public void setProductCatalogName(String productCatalogName) {
    this.productCatalogName = productCatalogName;
}

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getOperator_Code() {
    return Operator_Code;
}

public void setOperator_Code(String operator_Code) {
    Operator_Code = operator_Code;
}

public String getProduct_name() {
    return product_name;
}

public void setProduct_name(String product_name) {
    this.product_name = product_name;
}

public int getSubs_id() {
    return subs_id;
}

public void setSubs_id(int subs_id) {
    this.subs_id = subs_id;
}   



}

As you can see i have already use int variable for the primary key subs_id then why its giving exception Provided id of the wrong type for class tpay.table.ProductDetail. Expected: class java.lang.Integer, got class java.lang.String

Upvotes: 2

Views: 24020

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42114

Type of the id given to the session.get is wrong. As told by error message, String was given when Integer was expected:

Expected: class java.lang.Integer, got class java.lang.String

Such an error occurs with following code:

String subs_id = "1";
ProductDetail productDetail = (ProductDetail )
    session.get(ProductDetail .class, subs_id);

And with this code type is correct and no error occurs:

Integer subs_id = 1; //or primitive int
ProductDetail productDetail = (ProductDetail )
    session.get(ProductDetail .class, subs_id);

Upvotes: 4

Related Questions