Org.hibernate.AnnotationException: the collection has neither a generic type nor OneToMany.targetEntity ()

I used Hibernate Tools to create my POJO Hibernate mapping.

Unfortunately, the code generated by Hibernate tools doesn't seem to work, I get an exception

org.hibernate.AnnotationException: the collection has neither a generic type nor OneToMany.targetEntity ()

Parts of code that throw an exception

/** * 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 need to manually generate the hibernation code.

This is one thing I want to avoid. Any ideas what could be the problem?

Hi

Js

+7
source share
6 answers

I found a solution for me that works.

Just check "Use Java 5 Syntax" as shown in the attached screenshot and Hibernate Tools generate the correct generic types for collections.

enter image description here

+12
source

What the exceptions tell you is clear: yout @OneToMany collection must indicate a specific type ( Set<AnotherEntity> ) or have @OneToMany(targetEntity=AnotherEntity.class)

+6
source

Using Java 5 syntax is the correct answer ... but it is very misleading. Must be used Java 5+ Syntax .. who would have thought to use java 5.

+2
source

Search for "Red Hat hbm2java docs" because the link is broken. Added bonus, so you do not need to search. Select both of these checkboxes to “upgrade” the generated Java. Unfortunately, the flag labels do not indicate what will happen if you check them. You might think that “Use Java 5 Syntax” has many consequences, and since it is disabled by default, this is not a desirable / typical behavior.

Be sure to check both fields for the use of annotations and generics. Then tell Eclipse under the “clear” options to generate a serial version UID. Took me 3 days to understand all this.

+1
source

For those in need. As far as I remember, Java EE 5 introduced a lot of functionality in Java Enterprise Edition, especially in the use of annotations and generics. So, why I believe that checking “Use Java 5 Syntax” on Eclipse or “Java 5 Compatibility” (not sure about the exact expression in the user interface) in Netbeans when creating Entities whith Hibernate will ensure that the generated code will have these “new functionalities” features "Java 5 counting.

Since I have only 42 reputation, I can not comment !!!

+1
source

To use java 5 generics in auto-build, you can change your arguments to include jdk5 as follows:

 <hbm2java jdk5="true"> 

This information is here: Red Hat hbm2java docs .

0
source

All Articles