Main Tutorials

org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity()

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ganesh.M
12 years ago

While creating pojos using netbeans, check the option of “Use java 5 syntax”.So it generates a right syntax of Set type with generic.

Juan Carlos Rojas
4 years ago

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.

jomcy johny
4 years ago

Solved my issue

Jeremy S.
12 years ago

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

Jeremy S
12 years ago

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

Antonio Augusto
11 years ago
Reply to  Jeremy S

Thank you. This was solution my problem