我試圖在Spring中使用帶有Hibernate註解的ORM,但是當我想使它合併失敗時。Spring + Hibernate ORM註釋
我有兩個表(根據Java類):
產品(PK的productId,FK productGroup) ProductGroup(PK productGroupTri)
Product.java
@Entity
@Table(name="product")
public class Product implements Serializable{
@Id
@Column(name="product_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int productId;
@Column(name="product_name")
private String productName;
@OneToOne
@PrimaryKeyJoinColumn
private ProductGroup productGroup;
public void setProductId(int productId) {
this.productId = productId;
}
public int getProductId() {
return this.productId;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getproductName() {
return this.productName;
}
public void setProductGroup(ProductGroup productGroup) {
this.productGroup = productGroup;
}
public ProductGroup getProductGroup() {
return this.productGroup;
}
}
ProductGroup.java
@Entity
@Table(name="product_grp")
public class ProductGroup {
@Id
@Column(name="grp_tri")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private String productGroupTri;
@Column(name="grp_div")
private String productGroupDiv;
@OneToOne(mappedBy="productGroup", cascade=CascadeType.ALL)
private Product product;
public void setProductGroupTri(String productGroupTri) {
this.productGroupTri = productGroupTri;
}
public String getProductGroupTri() {
return this.productGroupTri;
}
public void setProductGroupDiv(String productGroupDiv) {
this.productGroupDiv = productGroupDiv;
}
public String getProductGroupDiv() {
return this.productGroupDiv;
}
public void setProduct(Product product) {
this.product = product;
}
public Product getProduct() {
return this.product;
}
}
而HQL請求者ST失敗:
String hql = "from Product as p where p.productId = :id";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setInteger("id", productId);
return (Product) query.uniqueResult();
我已經得到了以下消息:
org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.String, got class java.lang.Integer
我想獲得一個產品組的名字,我知道這是因爲主要的類型和外鍵,但我沒有發現任何問題,雖然我已經在論壇上搜索。
感謝您的幫助
我必須保持一個String此ID,以便你能告訴我,我怎麼能找到一種方法,解決問題,直到我有字符串? – tvirgil
希望[文檔](http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-identifier)可以幫助您 – Atropo
不是真的......註釋似乎是正確的,我應該罰款id類型的問題......但是,謝謝 – tvirgil