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 the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
Ganesh.M
Guest
Ganesh.M

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

Jeremy S.
Guest
Jeremy S.

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
Guest
Jeremy S

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
Guest
Antonio Augusto

Thank you. This was solution my problem