2 Replies Latest reply on Jun 27, 2007 11:09 PM by Bram Biesbrouck

    bidirectional OneToMany throws LazyInitializationException

    Bram Biesbrouck Newbie

      Hi all,

      I've a specific problem with my EJB3 Hibernate code (using Seam, but I don't think this has anything to do with it).

      This is my case:

      I have a superclass entity bean: DefaultAccessControlList

      @Entity
      @Table(name="access_control_list")
      @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(
       name="type",
       discriminatorType=DiscriminatorType.STRING
      )
      @DiscriminatorValue("default")
      public class DefaultAccessControlList implements Serializable
      {
       //-----CONSTANTS-----
       private static final long serialVersionUID = -1406511547129599450L;
      
       //-----VARIABLES-----
       protected int id;
      
       //-----CONSTRUCTORS-----
       public DefaultAccessControlList()
       {
       }
      
       //-----GETTERS/SETTERS------
       @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       public int getId()
       {
       return id;
       }
       protected void setId(int id)
       {
       this.id = id;
       }
      


      with two subclasses:
      @Entity
      @Name("principalAccessControlList")
      @DiscriminatorValue("principal")
      public class PrincipalAccessControlList extends DefaultAccessControlList implements Serializable
      {
       //-----CONSTANTS-----
       private static final long serialVersionUID = -6044077966387630697L;
      
       //-----VARIABLES-----
       private Inode inode;
      
       //-----CONSTRUCTORS-----
       public PrincipalAccessControlList()
       {
       }
      
       //-----GETTERS/SETTERS------
       @ManyToOne(targetEntity=AbstractInodeImpl.class)
       @JoinColumn(name="inode")
       public Inode getInode()
       {
       return inode;
       }
       public void setInode(Inode inode)
       {
       this.inode = inode;
       }
      }
      


      and

      @Entity
      @Name("groupAccessControlList")
      @DiscriminatorValue("group")
      public class GroupAccessControlList extends DefaultAccessControlList implements Serializable
      {
       //-----CONSTANTS-----
       private static final long serialVersionUID = -2681771597593712778L;
      
       //-----VARIABLES-----
       private Inode inode;
      
       //-----CONSTRUCTORS-----
       public GroupAccessControlList()
       {
       }
      
       //-----GETTERS/SETTERS------
       /**
       * Note: this side is the owner side of this relationship
       */
       @ManyToOne(targetEntity=AbstractInodeImpl.class)
       @JoinColumn(name="inode")
       public Inode getInode()
       {
       return inode;
       }
       public void setInode(Inode inode)
       {
       this.inode = inode;
       }
      }
      


      These beans are the owning side of the Inode-ACL relationship, and the Inode (abstract) class is like this:

      @Entity
      @Table(name="inode")
      @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(
       name="type",
       discriminatorType=DiscriminatorType.STRING
      )
      public abstract class AbstractInodeImpl implements Inode
      {
       ...
      
       @OneToMany(mappedBy="inode")
       public List<PrincipalAccessControlList> getPrincipalAccessControlLists()
       {
       return principalAccessControlLists;
       }
       public void setPrincipalAccessControlLists(List<PrincipalAccessControlList> principalAccessControlLists)
       {
       this.principalAccessControlLists = principalAccessControlLists;
       }
       @OneToMany(mappedBy="inode")
       public List<GroupAccessControlList> getGroupAccessControlLists()
       {
       return groupAccessControlLists;
       }
       public void setGroupAccessControlLists(List<GroupAccessControlList> groupAccessControlLists)
       {
       this.groupAccessControlLists = groupAccessControlLists;
       }
      
       ...
      }
      


      The problem is that, when I fetch an Inode-object, the reverse-relationship methods getGroupAccessControlLists() and getPrincipalAccessControlLists() are mixed up, and one of them throws an "org.hibernate.LazyInitializationException: illegal access to loading collection" exception when accessed.

      Any ideas?

      bram