-->
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: lazy data collection loading
PostPosted: Thu May 10, 2007 4:46 am 
Newbie

Joined: Thu May 10, 2007 4:19 am
Posts: 5
Dear Sirs

I need to initialize the POJO object from data base table that have a collection of values from another table. Everything is OK and an I bring all the data into the collection by many-to-many association.

But is it possible to tell of hibernate when load the collection? I want sometime to initialize the object without loading the collection element.

I tried it by setting property lazy="true":

<set name="roles" table="test_user_roles" inverse="false" lazy="true">
<key column="user_id"/>
<many-to-many column="role_id" class="com.piconsult.model.Role"/>
</set>

It seem to work but when I verify the collection is loaded.

It is the source that load data from database:

HibernateTemplate ht = getHibernateTemplate();

Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
TransactionSynchronizationManager.bindResource(this.getSessionFactory(), new SessionHolder(session));

User user = (User)ht.get(User.class, id);
System.out.println("roles size: " + user.getRoles().size()); //- here I want to have a collection with 0 elements
if (user == null)
{
throw new ObjectRetrievalFailureException(User.class, id);
}
TransactionSynchronizationManager.unbindResource(this.getSessionFactory());
SessionFactoryUtils.releaseSession(session, this.getSessionFactory());
return user;

Regards
Kamen


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 4:54 am 
Beginner
Beginner

Joined: Mon Nov 06, 2006 2:40 am
Posts: 29
Location: New Delhi, India
hi,

the problem is wth following part of code

Code:
System.out.println("roles size: " + user.getRoles().size());


when you call size method on set, at that time it loads the data from data base, as you requested an operation on that data,

hibernate has its own implemntation of Set, List and other collection types.

Call the size method after closing the session and check the results.


Rohit

-- Don't forget to rate :


Top
 Profile  
 
 Post subject: lazy loading
PostPosted: Thu May 10, 2007 7:01 am 
Newbie

Joined: Thu May 10, 2007 4:19 am
Posts: 5
Thank You sir

It is really the reason.

Best regards
Kamen


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 3:54 am 
Beginner
Beginner

Joined: Mon Nov 06, 2006 2:40 am
Posts: 29
Location: New Delhi, India
please rate if it helped.. :)


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.