2016-08-15 1 views
0

Hier ist meine SQL-Tabelle Beschreibungbereitgestellt ID vom falschen Typ für die Klasse classname Erwartet: Klasse java.lang.Integer, bekam Klasse java.lang.String

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  |  | 
+--------------------+-------------+------+-----+---------+-------+ 

Jedes Mal, wenn ich die get rufen

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

ich bin immer Ausnahme:

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; 
} 



} 

Wie Sie verwende ich sehen bereits int-Variable für den Primärschlüssel subs_id dann, warum seine geben Ausnahme id vom falschen Typ für die Klasse tpay.table.ProductDetail bereitgestellt. Erwartet: Klasse java.lang.Integer, bekam Klasse java.lang.String

Antwort

2

Art der ID der session.get gegeben ist falsch. Wie durch Fehlermeldung gesagt, String gegeben wurde, als Integer erwartet wurde:

Erwartet: Klasse java.lang.Integer, bekam Klasse java.lang.String

Ein solcher Fehler mit folgendem Code auftritt:

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

und mit diesem Codetyp ist korrekt und tritt kein Fehler:

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

Dank für das th Die Antwort Sir, du hast Recht, ich habe es schon selbst gelöst, es war ein dummer Fehler –

Verwandte Themen