-->
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.  [ 27 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Lazy loading of a property doesn't work
PostPosted: Fri Feb 11, 2005 2:52 pm 
Beginner
Beginner

Joined: Fri Feb 11, 2005 2:40 pm
Posts: 27
I am trying to use the lazy loading of properties with Hibernate 3.0. I have a class Copay that has several properties - id, balance, date and comments. I'm trying to make comments lazily loaded by adding lazy="true" to the mapping document. However, when I look at the generated SQL query it still shows that all columns are selected (I was expecting that comments property would not be loaded until you actually try to access it). The SQL doesn't seem to change when you change lazy from true to false. Is this feature not implemented or is there something wrong with my approach?

Hibernate version: 3.0

Mapping documents:

MAPPING FILE:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
<class
name="db.hibernateSimple.Copay"
table="EHR_COPAY"
polymorphism="explicit"
>

<id
name="id"
column="COPAY_ID"
type="long"
unsaved-value="null"
>
<generator class="sequence">
<param name="table">HIVAL</param>
<param name="column">NEXT</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Copay.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<property
name="balance"
type="int"
column="balance"
length="10"
/>

<property
name="date"
type="java.lang.String"
column="OWNERSHIP_DATE"
/>

<property
name="comments"
type="java.lang.String"
column="comments"
lazy="true"
/>

<many-to-one
name="user"
class="db.hibernateSimple.User"
cascade="none"
outer-join="auto"
column="USER_ID_SEQ"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Copay.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Session s = DataContext.getInstance().getSession();
System.out.println("Got session, creating a query...");
Query query = s.createQuery("from User user where user.lastName=:name");
query.setParameter("name", name);
User user = (User)query.list().get(0);
System.out.println("Got user");

int copaysSize = HibernateTestDAO.getInstance().getCopaysSize(user);
System.out.println("User " + name + " has " + copaysSize + " copays");


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):

Debug level Hibernate log excerpt:


LOG EXCERPT:
Got session, creating a query...
18:15:42,846 DEBUG SQL:297 - select user0_.USER_ID_SEQ as USER_ID_1_, user0_.LAST_NAME as LAST_NAME1_ from USERS user0_ where
(user0_.LAST_NAME=? )
Got user
18:15:43,256 DEBUG SQL:297 - select count(copay0_.COPAY_ID) as col_0_0_ from EHR_COPAY copay0_ where (copay0_.USER_ID_SEQ=? )

User has 1 copays
18:15:43,557 DEBUG SQL:297 - select copays0_.USER_ID_SEQ as USER_ID_5___, copays0_.COPAY_ID as COPAY_ID__, copays0_.COPAY_ID
as COPAY_ID0_, copays0_.balance as balance0_0_, copays0_.OWNERSHIP_DATE as OWNERSHI3_0_0_, copays0_.comments as comments0_0_,
copays0_.USER_ID_SEQ as USER_ID_5_0_0_ from EHR_COPAY copays0_ where copays0_.USER_ID_SEQ=?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 11, 2005 2:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Are you doing the necessary compile-time enhancement?


Top
 Profile  
 
 Post subject: Lazy loading doesn't work for properties and components
PostPosted: Fri Feb 11, 2005 3:42 pm 
Beginner
Beginner

Joined: Fri Feb 11, 2005 2:40 pm
Posts: 27
michael wrote:
Are you doing the necessary compile-time enhancement?


Sorry, forgot to mention it. Yes, I'm running this Ant task:

<target name="instrument" depends="compile">
<taskdef name="instrument" classname="org.hibernate.tool.instrument.InstrumentTask">
<classpath path="${classes.dir}"/>
<classpath refid="class.path"/>
</taskdef>
<instrument verbose="true">
<fileset dir="${classes.dir}/db/hibernateSimple">
<include name="*.class"/>
</fileset>
</instrument>
</target>

I get the following output:

instrument:
[instrument] Enhancing class db.hibernateSimple.User
[instrument] Enhancing class db.hibernateSimple.CopayDetails
[instrument] Enhancing class db.hibernateSimple.Copay
[instrument] Enhancing class db.hibernateSimple.HibernateTestDAO
[instrument] Enhancing class db.hibernateSimple.DataContext
[instrument] Enhancing class db.hibernateSimple.HibernateTest

I've tried changing the lazy loading of components and it doesn't seem to work either. Has anyone tried lazy loading with instrumentation?

Another workaround would be to just create two classes that are mapped to the same table, for instance UserSummary and UserDetails. UserSummary will have a one-to-one association with UserDetails. The idea is that when we need to display a table of users we don't want to load all of their information. When we need to display the full information on a user, we have to load all columns (hence the use of UserDetails object). Is there a better way of doing it? I'm not particularly keen on bytecode instrumentation anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2005 7:23 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Running with hsqldb and JDK5

i have something strange with lazy property too.
Last month all worked fine.

Here is a sum of my tests.
With H3 b4:
- lazy property throws an arrayOutOfBoundException
- lazy formula works fine

With last CVS + homebuild:
- lazy property AND lazy formula are too lazy, they never get loaded, in the debugger i can see the cglib stuff. All this results in a "update set null" on flush, lazy properties seems to be considered as null.

I continue testing before submitting to jira.

Note: without instrumentation, i have no bug (but no lazy behaviour of course). Has the InstrumentTask changed ?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2005 7:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
ok i got the answer to my first problem in the javadoc
Quote:
* <em>Due to a bug in CGLIB, this task will fail with array

* out of bounds if the passed fileset contains things

* that are not compiled java classes. Also, it appears

* that there is a problem with instrumenting classes

* twice, if the ant task is re-run!</em>


let's continue on the second problem

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2005 7:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yeah, looks like a broke something in the last couple of days.

(Damn, I wish I could run those tests inside eclipse...)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2005 9:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Fixed in CVS.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 5:40 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
ok i was only able to update from cvs now so i can test it again ;)

Gavin,
it seems ok for me too but can you validate how lazy property works.
Let's say i have declared 3 properties as lazy:
- will all lazy properties be fetched when the first is hit ?
- or will they be fetched one by one just as collection or many-to-one associations ?

When i run my test, i have the first behaviour.

Just tell me if it is the expected behaviour.

See u

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 6:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
All together, much more efficient.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 6:07 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
nice, i just wanted to be sure.
I think i'll never use this (except for lazy heavy formula) but it's good to know ;)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Still doesn't work with rc1
PostPosted: Wed Feb 23, 2005 2:11 pm 
Beginner
Beginner

Joined: Fri Feb 11, 2005 2:40 pm
Posts: 27
anthony wrote:
ok i was only able to update from cvs now so i can test it again ;)

Gavin,
it seems ok for me too but can you validate how lazy property works.
Let's say i have declared 3 properties as lazy:
- will all lazy properties be fetched when the first is hit ?
- or will they be fetched one by one just as collection or many-to-one associations ?

When i run my test, i have the first behaviour.

Just tell me if it is the expected behaviour.

See u


I have gotten the latest version from CVS too (rc1) and retried on my example. Now I don't get ArrayOutOfBounds exception and when I set lazy="true" for a property it is not selected in the initial object load (as expected behavior). However, if I try to get the value of the property, I get a null even though there is a value in the database. If I turn off lazy loading for that property, it is loaded in the initial SELECT and working correctly.

Am I supposed to do something for the class mapping? So far I have the same class mapping and I'm just flipping lazy="true/false" for a property. I was expecting to see an extra SELECT when I try to get the value of the property. My class mapping is shown at the top of the post.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 2:39 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
anthony wrote:
ok i got the answer to my first problem in the javadoc
Quote:
* <em>Due to a bug in CGLIB, this task will fail with array

* out of bounds if the passed fileset contains things

* that are not compiled java classes. Also, it appears

* that there is a problem with instrumenting classes

* twice, if the ant task is re-run!</em>


let's continue on the second problem


Do you need nned this task form CGLIB ? Probably it will be better to fork it, but I will fix file filter it if it makes sence for hibernate and if it not fixed in current version. As I understand Hibernate needs more functionality in this task like *.jar files. CGLIB become too stable and boring we can add more stuff if you are going to use it. I can help to implement "lazy" delete and update without reads too.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 4:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I worked around these "bugs" in the current H3 cvs.

basically only acception .class files and use ClassReader to check if the classes already implements the interface.

But AFAIK it would be better if this stuff were fixed in the cglib package.
Since I dont think just checking for implements the proper interface is 100% safe.

And if we could do it for jars also it would be great.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 4:08 pm 
Beginner
Beginner

Joined: Fri Feb 11, 2005 2:40 pm
Posts: 27
I hate to bug you guys but I am not sure if anyone has seen my second message. The lazy property loading still doesn't eem to work for me. Does it work for everyone else? Can somone comment on my previous post in this topic?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 4:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it works fine in the hibernate 3 unit tests...have you played around with those to see it work ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 27 posts ]  Go to page 1, 2  Next

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.