2 Replies Latest reply on Dec 17, 2009 4:24 PM by blabno

    BiDirectional Entities

    jimic79

      I'm working on an application that was set up by someone else in our company.  He says this is correct, but I don't believe so.  Please settle the argument.


      We have Groups and Users.  Groups has a list of users, users have a list of groups.  Below are the tops of both entities:





      @Entity
      @Name("usergroup")
      public class UserGroup extends AbstractEntity
      {
              private static final long serialVersionUID = 1L;
      
              private String name;
              
              @OneToMany(mappedBy="groups")
              @JoinTable(joinColumns={@JoinColumn(name="groups_id")})
              private List<User> members = new ArrayList<User> ();
      ...





      @Entity
      @Name("user")
      @Scope(ScopeType.SESSION)
      public class User extends AbstractEntity
      {
              private static final long serialVersionUID = 1L;
      
              @Column(unique=true)
              private String username;
              private String password;
              
              @OneToMany
              @JoinTable(joinColumns={@JoinColumn(name="members_id")})
              private List<AuthGroup> groups = new ArrayList<AuthGroup> ();
      ...



      AbstractEntity just has the id and uid properties for entities so we weren't replicating code.


      Should both mappings be @OneToMany?  are the joins done correctly?  do we need to define updatable/insertable like this post says: Bidrectional List


      Also, should user be ScopeType.SESSION?  It's used for actual authentication into our application.


      I just think this is causing a lot of our issues, because we have to always do stuff like:



      user.getGroups().add(newGroup);
      newGroup.getMembers().add(user);



        • 1. Re: BiDirectional Entities
          jimic79

          I was editing the code to make it more readable, we actually call users auths for internal reasons, so, according to my changes:



          private List<AuthGroup> groups = new ArrayList<AuthGroup> ();





          should be



          private List<UserGroup> groups = new ArrayList<UserGroup> ();




          on the User entity... sorry for the confusion.

          • 2. Re: BiDirectional Entities
            blabno

            This should be ManyToMany relationship.