1 Reply Latest reply on Aug 18, 2005 8:36 PM by Emmanuel Bernard

    many-to-many problem

    Zihong Lu Newbie

      My jboss version: 4.0.3RC1, with EJB3 beta1

      I have a many-to-many relationship defined as:

      public class Group extends ModelBase
      @ManyToMany(fetch = FetchType.LAZY)
      @JoinTable(table = @Table(name = "UserGroup"),
      joinColumns = {@JoinColumn(name = "GroupId")},
      inverseJoinColumns = {@JoinColumn(name = "UserId")})
      transient private Collection users = null;

      // entity properties
      // ....

      public class User extends ModelBase
      @ManyToMany(fetch = FetchType.LAZY, mappedBy="users")
      transient private Collection groups = null;

      // entity properties
      // ....

      public abstract class ModelBase
      private Integer id = null;
      private Integer version = null;

      Note: the keyword "transient" here is intended to block the relationships from transporting to remote.

      This is the DAO that I have:

      class MyDAOBean
      List getUsers(int groupid)
      Group group = (Group) manager.find(Group.class, groupid);
      group.getUsers().size(); // to initialized the relationships
      return group.getUsers();

      When my remote client retrieves the User objects, the server side DAO bean retrieves users list. However, the client side gets an exception, when accessing the specific user object:

      15:09:24,906 ERROR [LazyInitializationException] could not initialize proxy - no Session
      org.hibernate.LazyInitializationException: could not initialize proxy - no Session
      at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:51)
      at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:85)
      at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:131)

      From eclipse debugger, I can see the retrieved list of users object are of type

      User$$EnhancerByCGLIB$$88dcf93d (id=24)

      instead of the intended type "user". It seems the intended initialization in the MyDAOBean.getUsers() did not do its job. This seems wrong to me. From Hibernate, I learnt that specifically call the collection.size() will force the relationship to be initialized. Is this still the case in EJB3? If not, what is the way to force initialization, besides using specific "fetch join" EJB query?

      I also tried to change the relationship owner, from Group.users to User.groups, it still behave the same.

      I used the same Java transient keyword in the one-to-many relationship, when the client retrieve the children (just like retrieve users in this many-to-many case), the children type was correct, it seems to work fine.