Problem
See following Hibernate mapping via annotation, which is generated by NetBean’s Hibernate Tool.
File : Stock.java
package com.mkyong.stock.model;
//...
@Entity
@Table(name = "stock", catalog = "mkyong");
public class Stock implements java.io.Serializable {
//...
private Set stockCategories = new HashSet(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "stock")
public Set getStockCategories() {
return this.stockCategories;
}
public void setStockCategories(Set stockCategories) {
this.stockCategories = stockCategories;
}
}
File : StockCategory.java
package com.mkyong.stock.model;
//...
@Entity
@Table(name = "stock_category", catalog = "mkyong");
public class StockCategory implements java.io.Serializable {
private Stock stock;
//...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "STOCK_ID", nullable = false)
public Stock getStock() {
return this.stock;
}
}
When program is running, it hit following exception :
Caused by: org.hibernate.AnnotationException:
Collection has neither generic type or OneToMany.targetEntity()
defined: com.mkyong.stock.model.Stock.stockCategories
Solution
Hibernate does not support relationship which is in raw type, and since your “Set stockCategories” didn’t declared any of the data type, and Hibernate unable to proceed on it.
To fix it, just declare the exact data type in your relationship variable which has a @OneToMany annotated, see below.
File : Stock.java
package com.mkyong.stock.model;
//...
@Entity
@Table(name = "stock", catalog = "mkyong");
public class Stock implements java.io.Serializable {
//...
private Set<StockCategory> stockCategories = new HashSet(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "stock")
public Set getStockCategories() {
return this.stockCategories;
}
public void setStockCategories(Set<StockCategory> stockCategories) {
this.stockCategories = stockCategories;
}
}
While creating pojos using netbeans, check the option of “Use java 5 syntax”.So it generates a right syntax of Set type with generic.
Eclipse + Hibernate plugin is able to generate the Hibernate code as well 🙂
Thanks for the help, it is valuable. I had found an example on the internet: @OneToMany(mappedBy=”mainMenu”, cascade = CascadeType.ALL)
Set subMenu = new HashSet();
and was worried because it works, but this error appeared to me. With your help it was solved.
Solved my issue
BTW
I created my POJO files via Hibernate Tools and this lead to the mentioned error.
Later on I discovered that I was only missing the right settings in Hibernate Tools Code Generation Wizard…
you have to set “Use Java 5 Syntax”.
Then the Wizard generates the right Code with
Set var = new HashSet(0)
for you.
Regards JS
Code in File: Stock.java
should also add generic type to getStockCategories()
package com.mkyong.stock.model; //... @Entity @Table(name = "stock", catalog = "mkyong"); public class Stock implements java.io.Serializable { //... private Set stockCategories = new HashSet(0); @OneToMany(fetch = FetchType.LAZY, mappedBy = "stock") public Set getStockCategories() { return this.stockCategories; } public void setStockCategories(Set stockCategories) { this.stockCategories = stockCategories; } }Thank you. This was solution my problem