-->
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: Lazy true not being honoured
PostPosted: Thu May 12, 2005 8:06 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 5:07 am
Posts: 37
Location: Cambridge, UK
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.0.3

Mapping documents:

<hibernate-mapping default-lazy="true">

<class
name="com.mycompany.myapplication.pojo.TopLevelPOJO"
table="TOPLEVELPOJO"
schema="MYSCHEMANAME"
>

<many-to-one
name="assignedTo"
class="com.mycompamy.myapplication.data.pojo.MyForeignKeyPojo"
not-null="false"
outer-join="false"
lazy="true"
>
<column name="FOREIGN_KEY_POJO_ID" />
</many-to-one>



Code between sessionFactory.openSession() and session.close():

Criteria criteria = session.createCriteria(MyTopLevel.class);
criteria.addOrder(Order.desc("creationDate"));
criteria.setMaxResults(50);
return criteria.list();



Full stack trace of any exception that occurs:

Name and version of the database you are using: Oracle 9

The generated SQL (show_sql=true):

The SQL is first executed fine which gets the top level class, but then
SQL is executed to load the foreign key pojo, despite lazy="true"..



Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 3:43 am 
Newbie

Joined: Fri May 13, 2005 3:32 am
Posts: 3
Location: Moscow, Russia
I noticed the same problem with Hibernate 3.0.3. It seems it retrieves elements for the <many-to-one> association even if they're marked as lazy. Hibernate 3.0.3 introduces a new type of lazy loading for <many-to-one>: lazy="proxy" that is default now. Try it.


Top
 Profile  
 
 Post subject: many-to-one Lazy=proxy
PostPosted: Tue May 17, 2005 3:32 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 5:07 am
Posts: 37
Location: Cambridge, UK
Thanks, this works for me... but why does Lazy=true not work?

What is lazy=proxy actually doing? are there any decent docs on this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 4:39 am 
Newbie

Joined: Fri May 13, 2005 3:32 am
Posts: 3
Location: Moscow, Russia
It's stated in the reference documentation for 3.0.3. See section 6.1.10


Top
 Profile  
 
 Post subject: Hibernate Proxy
PostPosted: Wed May 18, 2005 5:29 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 5:07 am
Posts: 37
Location: Cambridge, UK
ok, so Hibernate is going to subclass my object at runtime (using the CGLIB package), mirror my object, and load it when I call a getter my object..

I thought this was what lazy loading does anyway?

and the strange thing is also that I am pretty sure, with lazy=true set, I was not calling the get method on my object anyway - so I am still not sure why it was loading it eagerly... ?


Top
 Profile  
 
 Post subject: Hibernate Proxy
PostPosted: Wed May 18, 2005 5:51 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 5:07 am
Posts: 37
Location: Cambridge, UK
ok, "supposing that has a collection of Nutter objects that is lazily loaded. If you call looneyBin.getNutters(), Hibernate will only load the ID and version number of the Nutter objects, thus saving all the work of loading individual objects until later. You think you have all the related Nutter objects, but what you really have is a collection of proxies. The thinking is that if you have a collection of a few hundred items, chances are good that you'll only work with a few of them, so why go through the effort of instantiating all the objects. This can be a huge performance gain in certain situations."


ok, so lazy=proxy is going to lazily load just the id and version of my collection objects (in my many-to-one)... but that still does not explain why lazy=true executes SQL for my many-to-one and lazy=proxy does not.. (when I am not even accessing my collection..)

I can only presume it is a bug?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 7:50 am 
Newbie

Joined: Fri May 13, 2005 3:32 am
Posts: 3
Location: Moscow, Russia
I suppose it's a bug in 3.0.3. At least doc states that Hibernate should fetch a lazy many-to-one association only if you call its get method explicitly.


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.