-->
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: fetching many-to-many collection
PostPosted: Sun Oct 07, 2007 3:48 am 
Newbie

Joined: Sun Oct 07, 2007 3:31 am
Posts: 2
Hi,
I've been using Hibernate for a while but every now and then there is something I just don't understand - like this:

This is the mapped class (importang part separated):
Code:
<hibernate-mapping default-lazy="false" default-cascade="none" default-access="property" auto-import="true">
<class name="de.infrage.server.model.Ressource" table="ressources" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version">
<id name="id" column="id" type="java.lang.Integer" unsaved-value="null">
<generator class="native">
  </generator>
  </id>
  <property name="mimetype" type="java.lang.String" update="true" insert="true" column="mimetype" unique="false" optimistic-lock="true" lazy="false" generated="never" />
  <property name="comment" type="java.lang.String" update="true" insert="true" column="comment" unique="false" optimistic-lock="true" lazy="false" generated="never" />
  <property name="size" type="long" update="true" insert="true" column="size" unique="false" optimistic-lock="true" lazy="false" generated="never" />
  <many-to-one name="ressourceFile" class="de.infrage.server.model.RessourceFile" cascade="all" outer-join="auto" update="true" insert="true" column="file_id" unique="false" optimistic-lock="true" not-found="exception" embed-xml="true" />
-------------------------------------------------------------
<set name="tags" table="ressources_tags" lazy="false" cascade="none" sort="unsorted" inverse="false" mutable="true" optimistic-lock="true" embed-xml="true">
  <key column="ressource_id" on-delete="noaction" />
  <many-to-many class="de.infrage.server.model.Tag" column="tag_id" outer-join="auto" embed-xml="true" not-found="exception" unique="false" />
</set>
-------------------------------------------------------------
<joined-subclass name="de.infrage.server.model.NewspaperArticle" table="newspaper_articles" dynamic-update="false" dynamic-insert="false" select-before-update="false">
  <key column="ressource_id" on-delete="noaction" />
  <property name="newspaper" type="java.lang.String" update="true" insert="true" column="newspaper" unique="false" optimistic-lock="true" lazy="false" generated="never" />
  <property name="date" type="java.util.Date" update="true" insert="true" column="date" unique="false" optimistic-lock="true" lazy="false" generated="never" />
  <property name="issue" type="java.lang.Integer" update="true" insert="true" column="issue" unique="false" optimistic-lock="true" lazy="false" generated="never" />
  </joined-subclass>
  </class>
  </hibernate-mapping>


What troubles me is the set/many-to-many relation. I set it to lazy="false", but loading a ressource, closing the session and accessing "tags" aftwerwards gives me...

org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed

... as I would expect in the case of lazy="false".

I tried a join fetch ("from Ressource as res left outer join fetch res.tags") instead, but then I get each Ressource as often as there are Tags in it - like a "real" SQL outer join.

Help much appreciated,
meikel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 08, 2007 8:04 am 
Newbie

Joined: Mon Oct 01, 2007 8:59 am
Posts: 18
this problem is due to the closing of session.
You should manage session carefully.

After each complete request session must be closed.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 08, 2007 12:00 pm 
Newbie

Joined: Sun Oct 07, 2007 3:31 am
Posts: 2
OK, I found the solution. After setting default-lazy="false" also for the Tag class it worked. Hmm, somewhat logical in hindsight...

Thanks,
Meikel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 08, 2007 6:50 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 3:22 am
Posts: 69
Lucky you I have and still am encountering this problem. Try fetching multiple collections in one entity and see how far you get. Hibernate does not support more then one eager fetch in an entity for some odd reason and as usual I can't get any answers on this board.


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.