org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity()
Problème
Voir le mappage Hibernate suivant via l'annotation, qui est généré par l'outil Hibernate de NetBean.
Fichier: Stock.java
package com.example.stock.model; //... @Entity @Table(name = "stock", catalog = "example"); 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; } }
Fichier: StockCategory.java
package com.example.stock.model; //... @Entity @Table(name = "stock_category", catalog = "example"); 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; } }
Lorsque le programme est en cours d'exécution, il a atteint l'exception suivante:
Caused by: org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity() defined: com.example.stock.model.Stock.stockCategories
Solution
Hibernate does not support relationship which is in raw type, et puisque votre «Set stockCategories
» n'a déclaré aucun des types de données et Hibernate n'a pas pu continuer dessus.
Pour résoudre ce problème, déclarez simplement le type de données exact dans votre variable de relation qui a un@OneToMany
annoté, voir ci-dessous.
File : Stock.java
package com.example.stock.model; //... @Entity @Table(name = "stock", catalog = "example"); public class Stock implements java.io.Serializable { //... private SetstockCategories = new HashSet(0); @OneToMany(fetch = FetchType.LAZY, mappedBy = "stock") public Set getStockCategories() { return this.stockCategories; } public void setStockCategories(Set stockCategories) { this.stockCategories = stockCategories; } }