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

Jeremy S. picture Jeremy S. · Aug 4, 2011 · Viewed 12.1k times · Source

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

Answer

Jeremy S. picture Jeremy S. · Aug 5, 2011

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