我尝试在 Spring 中将 ORM 与 Hibernate 注释一起使用,但是当我想制作关节时它失败了。
我有两个表(根据java类):
产品 (PK productId, FK productGroup) 产品组 (PK productGroupTri)
产品.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;
}
}
产品组.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 请求:
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
我想访问产品的组名,我知道这是因为主键和外键的类型,但是尽管我已经在论坛上进行了搜索,但我没有发现任何问题。
谢谢你的帮助