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;
    }
 
}
Tags :
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.
My website mkyong.com is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts