2 Replies Latest reply on Feb 9, 2008 8:56 AM by Micha Roon

    Using own Classes as member for ManyToMany-Assotiations

    Micho Edershvili Newbie

      I defined a class

      public class UsergroupList extends ArrayList<Usergroup>

      The class user is
      public class User implements Serializable
      { ...
       private UsergroupList mUsergroupList;
       @ManyToMany ( targetEntity = Usergroup.class )
       public Collection getUsergroupList()
       { return mUsergroupList; }

      when deploying I get the error
      org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.xyz.UsergroupList

      but I think UsergroupList implements collection as it is an ArrayList

      If I write
      @ManyToMany ( targetEntity = Usergroup.class )
       public Collection getBenutzerGruppeListe()
       { return mUsergroupList; }
      the deployment works.

      But invoking
      private EntityManager em;
      erg = (List<User>) em.createQuery("from User").getResultList();

      javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: Exception occurred inside setter of com.xyz.User.usergroupList

        • 1. Re: Using own Classes as member for ManyToMany-Assotiations
          Dalibor Kalna Newbie

          Hi micho,

          IMHO you ain't free to implement your own Collections for EJB3 associations, you have to stick with those in java.util package.

          hm, but I'm not sure...


          • 2. Re: Using own Classes as member for ManyToMany-Assotiations
            Micha Roon Newbie

            After reading your post (I had the same problem) I tried every descendant of java.util.Collection. It worked only with java.util:collection itself.

            the nice thing is that without specifying anything, JBoss created a link table between the two entities. Which is what should be done.


            * @return the roles
            cascade={CascadeType.PERSIST, CascadeType.MERGE}
            public Collection < Role > getRoles() {
            if(roles == null) {
            roles = new LinkedList < Role >();
            return roles;

            * this setter must exist for hibernate but be private because we don't want anyone to use it
            * @param roles the roles to set
            private void setRoles(Collection < Role > roles) {
            this.roles = roles;

            When the Roles are read from the database, the Class used for the roles Collection is org.hibernate.collection.PersistentBag

            It would actually be nice to be able to choose a type that disallows duplicates in the list. Because now I have to catch the exception upon "persist".