Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 536
Jeremy S. Avatar asked Aug 04 '11 07:08

Jeremy S.


2 Answers

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

like image 66
Bozho Avatar answered Oct 16 '22 21:10

Bozho


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

like image 31
levolutionniste Avatar answered Oct 16 '22 20:10

levolutionniste