org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity()
I used Hibernate Tools to generate my Hibernate POJO mapping.
Unfortunately the code generated by Hibernate tool开发者_C百科s seems not to work, I get the exception
org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity()
The code parts that generate the exception are
/**
* ClassFlag generated by hbm2java
*/
@Entity
@Table(name = "class_flag", catalog = "incbszdb")
public class ClassFlag implements java.io.Serializable {
....
/* HERE */
private Set classFlagI18ns = new HashSet(0);
/* HERE */
public void setClassFlagI18ns(Set classFlagI18ns) {
this.classFlagI18ns = classFlagI18ns;
}
}
According to this post
http://www.mkyong.com/hibernate/org-hibernate-annotationexception-collection-has-neither-generic-type-or-onetomany-targetentity/comment-page-1/#comment-67404
and this post
http://www.mkyong.com/hibernate/hibernate-error-collection-has-neither-generic-type-or-onetomany-targetentity/
You have to change Hibernates generated code by yourself by hand.
This is one thing I want to avoid. Any ideas what could be the problem?
Regards
JS
I have found a solution for me that works.
Simply check "Use Java 5 Syntax" as depicted in the attached Screenshot and Hibernate Tools generate the correct generic types for collections.
What the exceptions tells you is clear - yout @OneToMany
collection should either specify a concrete type (Set<AnotherEntity>
) or have @OneToMany(targetEntity=AnotherEntity.class)
For those who needs. As far as I remember, with Java EE 5, the Java Enterprise Edition got a lot of functionnalities, particularly in the use of annotations and generics. So That's why I think checking "Use Java 5 Syntax" on Eclipse, or "Java 5 Compatibility" (not sure of the exact term on the UI) on Netbeans , when generating the Entities whith Hibernate, will make sure the generated code will take those 'new functionnalities' of Java 5 in count.
Because I have just 42 as reputation I'm unable to comment!!!
Use Java 5 Syntax is the right answer... but it is very misleading. Should be Use Java 5+ Syntax.. who would think to use java 5.
In order to use java 5 generics in an automated build, you can modify your arguments to enable jdk5 like this:
<hbm2java jdk5="true">
This information is documented here: Red Hat hbm2java docs.
Search on "Red Hat hbm2java docs" because the link is broken. Added bonus so you don't have to search. Select both of those check boxes to "modernize" the generated Java. Unfortunately, the labels of the check boxes do not indicate what happens if you check them. One would think that "Use Java 5 syntax" has many implications and since it is off by default that it is not the desired/typical behavior.
By all means check both boxes to use annotations and generics. Then tell Eclipse under "clean up" options to generate the serial version UID. Took me 3 days to figure all this out.
精彩评论