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 making a donation to the charity, thanks.

Related Posts

Popular Posts