I tried to use ORM with Hibernate annotation in Spring but when i would like to make jointures it fails.
I have two tables (according to java class) :
Product (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;
}
}
And the HQL request that fails :
String hql = "from Product as p where p.productId = :id";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setInteger("id", productId);
return (Product) query.uniqueResult();
I’ve got the following message :
org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.String, got class java.lang.Integer
I would like to access to the group name of a product and I understand it’s because of the type of the Primary and Foreign Key but i didn’t find any issue although i’ve done search on forums.
Thanks for your help
@carbontax, i did this HQL :
Then, i find an issue to correct this error. In fact, i had this code to access to the column in my JSP :
And i correct this with :
I had also modify the cardinality in Product.java to ManyToOne And in ProductGroup.java to OneToMany.
The major problem is that i accessed to the object Product to get my property instead the object ProductGroup.
If someone got the same problem as me he can ask me help cause the issue is easy finally …