-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Exceptions when retrieving data via join
PostPosted: Fri May 20, 2016 2:48 pm 
Newbie

Joined: Thu Aug 29, 2013 6:43 am
Posts: 7
I have a User class with the following:

Code:
   @OneToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.LAZY)
   @PrimaryKeyJoinColumn
   UserProfile userProfile;

   @ManyToMany(cascade = { CascadeType.ALL })
   @JoinTable(name = "auth_user_groups", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "group_id") })
   private Set<Group> groups = new HashSet<Group>();


I can successfully load the user but much later in the program flow, I need to access the groups using user.getGroups(). Obviously the session that I used to load the user is no longer active.

So I thought I could open a new session and then get the groups, but I'm getting an exception:

Code:
      session = SessionManager.getDAOFactory().openSessionForModel("User");
      try {
         tx = session.beginTransaction();
         logger.debug("User groups: " + user.getGroups().toString());
      ....


Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: myapp.db.model.auth.User.groups, could not initialize proxy - no Session


I tried using initialize:

Code:
      session = SessionManager.getDAOFactory().openSessionForModel("User");
      try {
         tx = session.beginTransaction();
         Hibernate.initialize(user.getGroups());
      ....


But I'm getting a different exception:

Code:
org.hibernate.HibernateException: collection is not associated with any session
   at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:678)


I seem to be missing something in my understanding. Any suggestions appreciated.

Thanks


Top
 Profile  
 
 Post subject: Re: Exceptions when retrieving data via join
PostPosted: Fri May 20, 2016 2:58 pm 
Newbie

Joined: Thu Aug 29, 2013 6:43 am
Posts: 7
I searched a lot before I posted but I guess I didn't have the right search keywords. I just found a reference on binding the entity to the new session, so:

Code:
     session.update(user);
     logger.debug("User groups: " + user.getGroups().toString());


Which appears to work properly.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.