org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity()
Asked Answered
D

6

12

I used Hibernate Tools to generate my Hibernate POJO mapping.

Unfortunately the code generated by Hibernate tools 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

Deniable answered 4/8, 2011 at 7:57 Comment(0)
D
15

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.

enter image description here

Deniable answered 5/8, 2011 at 13:41 Comment(2)
What is the purpose of "use Java 5 syntax"? What are the downsides of not using it?Huss
@Dash the purpose of "use Java 5 syntax" is to add generics to your collections as far as I can see...Deniable
M
14

What the exceptions tells you is clear - yout @OneToMany collection should either specify a concrete type (Set<AnotherEntity>) or have @OneToMany(targetEntity=AnotherEntity.class)

Marcusmarcy answered 4/8, 2011 at 8:31 Comment(4)
yeah but this would be something I had to change "by-hand" after I generated the code via Hibernate Tools, this is something I want to avoid, otherwise everytime I change the DB and generate the POJO's via Hibernate Tools I have to make this changes myself ....Deniable
that's why you should not regenerate it. Do the other way around - use your object model as the base for db changesMarcusmarcy
because otherwise everytime I change something in the relational model (DB) I have to do all changes by hand which would be error prone...Deniable
thanks for your help, I have found a solution. Anyways I will vote you up because of your efforts. Regards JSDeniable
D
3

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!!!

Dendriform answered 30/6, 2016 at 14:11 Comment(0)
M
3

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.

Mauretania answered 29/9, 2016 at 12:44 Comment(0)
C
1

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.

Circumcise answered 5/7, 2013 at 14:14 Comment(0)
S
1

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.

Stutz answered 11/9, 2014 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.