-->
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.  [ 4 posts ] 
Author Message
 Post subject: unidirectional many-to-many no lazy initialization
PostPosted: Sun Feb 15, 2009 10:49 pm 
Newbie

Joined: Mon May 14, 2007 1:57 am
Posts: 12
Hi there,
I have two tables: user and roles. A user can have associated many roles, and this relationship is unidirectional. I want the roles to be fetched automatically, i tried using lazy="false" but that didn't work.
How can I set lazy to false for a many-to-many unidirectional relationship?
Thanks in advance!

Hibernate version: 3.2.6ga

Mapping documents:

Snippet of my User table mapping file (it doesn't work as I expected):
Code:
<bag name="roles" table="USER_ROL" lazy="false">
      <key column="USER_ID"/>
      <many-to-many class="Rol" column="ROL_ID" lazy="false"/>
</bag>


Full stack trace of any exception that occurs:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Name and version of the database you are using: MySQL


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 3:06 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The error is not related to initializing the <bag> with roles. It seems that it is related to initializing the proxies of the role entities themselves. You may try:

Code:
<bag ... fetch="join" ... >
  ...
  <many-to-many .... fetch="join" />
</bag>


If that doesn't work you may need to disable proxies for the role entity:

Code:
<class ... lazy="false">
...
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 6:31 pm 
Newbie

Joined: Mon May 14, 2007 1:57 am
Posts: 12
Thanks, it worked! I did:
Code:
<class ... lazy="false">
<bag ... fetch="join" >
  ...
  <many-to-many .... fetch="join" />
</bag>
</class>


but I don't get it... why it doesn't work when I set lazy="false" in <bag..> and in <many-to-many> tags?? I mean:
Code:
<class ... >
<bag ... lazy="false"  fetch="join" >
  ...
  <many-to-many .... lazy="false"  fetch="join" />
</bag>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 6:44 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It probably has something to do with that there is a difference between collection initialization and proxy initialization. Setting <bag lazy="false"> means that the collection will be initialized but it may be initialized with proxies. I hoped that <many-to-many .... fetch="join" /> would also initialize the proxies, but maybe it doesn't. It would be quite useful to keep the <class lazy="true"> but still has the option to override this for a specific collection.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.