3 Replies Latest reply on Aug 8, 2006 4:20 PM by Mark Menard

    Bidirectional One to Many, Many To One

    oliverchua Newbie

      Hi,

      I'm trying to follow the Hibernate Annotations Manual, under Collections
      2.2.5.3.2.1. Bidirectional
      Soldier and Troop classes...

      I tried creating the classes

      @Entity
      public class Troop {
       @Id
       @GeneratedValue
       private Long id;
       private String name;
       @OneToMany(mappedBy="troop")
       private List<Soldier> soldiers;
      }

      and
      public class Soldier {
       @Id
       @GeneratedValue
       private Long id;
       private String name;
       @ManyToOne
       @JoinColumn(name="troopFK")
       private Troop troop;
      }


      but I'm getting this error:
      java.lang.ExceptionInInitializerError
       at util.HibernateUtil.<clinit>(HibernateUtil.java:18)
       at onetomany.OneToMany.testOneToManyDefault(OneToMany.java:16)
       ...
      Caused by: org.hibernate.AnnotationException: Collection of elements must not have mappedBy or association reference an unmapped entity: onetomanypermutations.Troop.soldiers
       at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:717)

      ...

      I'm using Hibernate 3.2 and annotations 3.1beta9
      Anyone with an idea what's wrong?