-->
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.  [ 7 posts ] 
Author Message
 Post subject: mapping a lazy init many-to-many associtation
PostPosted: Mon Sep 08, 2008 5:51 am 
Newbie

Joined: Mon Sep 08, 2008 5:09 am
Posts: 6
Location: Cluj-Napoca
Hi,

Here a short description of my problem.
I have 2 entities named Tag and Seller. A tag has list of sellers and a seller has a list of tag. And I mapped this to a many-to-many association and everything is ok until now. Where I get an tag object all sellers are also retrieved, which is ok.

But I don't know exactly how to make this many-to-many association to be lazy="true". When I get a tag the sellers for that tag shouldn't be retrieve. Same with a seller, when I get a seller tags for that seller shouldn't be retrieved (because of the lazy init).
The problem is that this is nor working and I tried some sample, but with no result.

The mappings are like this:

<class name="Tag" table="tags">
......
<set name="sellers" table="tagssellers">
<key column="tagId"/>
<many-to-many class="Seller" column="sellerId"/>
</set>
...
</class>

===============================

<class name="Seller" table="sellers">
......
<set name="tags" table="tagssellers">
<key column="sellerId"/>
<many-to-many class="Tag" column="tagId"/>
</set>
...
</class>

Where should I add the lazy="true"? I tried adding it on both tags and sellers but it is not working.
It is possible to make both sellers and tags from each Tag and Seller entity to be lazy initialized?

Thanks,
Gabi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 6:29 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Lazy association fetching is enabled by default (unless you override with default-lazy attrib in hibernate-mapping tag). Are you sure you're not accessing the collections in any way e.g. tags.size() would cause the collection to be fetched. What version are you using.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 6:40 am 
Newbie

Joined: Mon Sep 08, 2008 5:09 am
Posts: 6
Location: Cluj-Napoca
I'm using hibernate3.
So you are saying that if I don't add lazy="true" on the tags it will anyway be lazy initializing?
I'm sure that I don't do a size() call or some other call, but I'm going to double check.
The default behavior when I tested was that for a tag all sellers where retrieved with the mappings that I posted earlier, where not lazy attribute was added . So I supposed that lazy is set to false.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 7:04 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I just checked the docs and lazy on the <set> element is:
"(4) lazy (optional - defaults to true) may be used to disable lazy fetching"

You could add a lazy="true" attribute to your set mappings just to be sure but I bet it won't fix the problem.

If you can, post the code of your Tag and/or Seller classes.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 4:17 pm 
Newbie

Joined: Mon Sep 08, 2008 5:09 am
Posts: 6
Location: Cluj-Napoca
So here a the code for the 2 classes. Tag and Seller.
And also the complete mappings and a code sample on how I retrieve a tag.

public class Tag extends Persistent {
//properties like id and name are inherited from persistent
/**
* The set of sellers that have this tag.
*/
private Set<Seller> sellers = new HashSet<Seller>();

//setter and getter
}


public class Seller extends Persistent {
/**
* The set of tags that were added on this seller
*/
private Set<Tag> tags = new HashSet<Tag>();

//setter and getter
}

==============================================
<hibernate-mapping>
<class name="Seller" table="Sellers" batch-size="10">
<id name="id" column="SID" type="java.lang.Long">
<generator class="hilo"/>
</id>

<set name="tags" inverse="true" cascade="save-update" table="TagsSellers">
<key>
<column name="SID"/>
</key>
<many-to-many class="Tag" column="id"/>
</set>

</class>
</hibernate-mapping>


<hibernate-mapping>
<class table="tags" name="Tag">
<id name="id" column="id">
<generator class="hilo"/>
</id>

<set name="sellers" cascade="save-update" table="TagsSellers">
<key>
<column name="id"/>
</key>
<many-to-many class="Seller" column="SID"/>
</set>

</class>
</hibernate-mapping>

===========================
Here is how I retrieve a seller and a tag
Seller seller = getManagerProvider().getSellerManager().getSeller(194314509L);

Tag tag = getManagerProvider().getTagManager().getTag(222494720L);

basically the getSeller/getTag calls something like
this : getObject(Tag.class, id);

So if I look at a tag's sellers they are filled. tag.getSellers() is not empty.
My problem is that is shouldn't be filled due to the lazy init that I want to use.


Thanks
Gabi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 5:14 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Quote:
So if I look at a tag's sellers they are filled. tag.getSellers() is not empty.

If you have an active session, simply looking at the tag's sellers (however that looking is done) will cause the sellers to be fetched from the database. You won't see an empty collection, lazy or not. If the session that loaded the tag has been closed when the collection is accessed then you'll get a lazy-initialization-exception if the collection is lazy.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2008 5:36 pm 
Newbie

Joined: Mon Sep 08, 2008 5:09 am
Posts: 6
Location: Cluj-Napoca
Yes. I think I understand.

Anyway, I supposed that my session was closed when I was doing getManagerProvider().getTagManager().getTag(222494720L); and that is way I was wondering why sellers collection is populated.

Thanks a lot for your time.


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