-->
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.  [ 10 posts ] 
Author Message
 Post subject: Property-ref and lazy loading
PostPosted: Mon May 16, 2005 12:37 pm 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
I think lazy loading do not work with property-ref ... or I do something wrong.

This is my case (simplified):

2 table/classe:
1)tst_main/tstMain
2)tst_extern_static/tstDACExternStatic

The 2nd has a unique constraint on "static_id" column, while "id" is the primary key (I haev to do this, so please don't suggest to use the primary key only, I know it should be better, but I can't).
There is a many-to-one relation between 1) and 2) on a static_id column, so I have to use
the property-ref keyword on mapping.
tstDACExternStatic is defined lazy but it do not work lazily because if I write:

String HQL = "FROM " + tstDACmain.class.getName();
Query q = hibSession.createQuery(HQL);
List res = q.list();

I see on the console:

Hibernate: select tstdacmain0_.id as id, tstdacmain0_.des as des0_, tstdacmain0_.id_extern_std as id3_0_, tstdacmain0_.id_extern_static as id4_0_ from tst_main tstdacmain0_
Hibernate: select tstdacexte0_.id as id0_, tstdacexte0_.des as des2_0_, tstdacexte0_.static_id as static3_2_0_ from tst_extern_static tstdacexte0_ where tstdacexte0_.static_id=?
Hibernate: select tstdacexte0_.id as id0_, tstdacexte0_.des as des2_0_, tstdacexte0_.static_id as static3_2_0_ from tst_extern_static tstdacexte0_ where tstdacexte0_.static_id=?
Hibernate: select tstdacexte0_.id as id0_, tstdacexte0_.des as des2_0_, tstdacexte0_.static_id as static3_2_0_ from tst_extern_static tstdacexte0_ where tstdacexte0_.static_id=?
Hibernate: select tstdacexte0_.id as id0_, tstdacexte0_.des as des2_0_, tstdacexte0_.static_id as static3_2_0_ from tst_extern_static tstdacexte0_ where tstdacexte0_.static_id=?

There are 5 reads ... 1 for the tst_main table (correct) and 4 for the tst_extern_static (wrong). There is 1 read for each row of tst_main.

This is wrong, since tstDACExternStatic is lazy and it depends from the property-ref tag, since if I remove it from mapping (so the relation will go on the primary key) it works perfecty:
I see only 1 SELECT statement.

This problem is very bad for my application since we must use propery-ref in many places, but if this cause the whole db (which will be huge) to be read, we must change all our architecture.

Regards
Alessandro Rizzi

Hibernate version:
3.0.3 or 2.1.6 (the behaviour is the same)

Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 1.1//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="testHib3.tstDACmain" table="tst_main">
<id column="id" name="id" type="long">
<generator class="native"></generator>
</id>
<property column="des" length="50" name="des" not-null="false" type="string"/>
<many-to-one name="externStd" column="id_extern_std" class="testHib3.tstDACExternStd"
cascade="none" outer-join="false" />
<many-to-one name="externStatic" column="id_extern_static"
class="testHib3.tstDACExternStatic" cascade="none"
outer-join="false"
property-ref="staticId"
lazy="true"/>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 1.1//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="testHib3.tstDACExternStatic" table="tst_extern_static" lazy="true">
<id column="id" name="id" type="long">
<generator class="native"></generator>
</id>
<property column="des" length="50" name="des" not-null="false" type="string"/>
<property column="static_id" unique="true" name="staticId" not-null="false" type="long"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
String HQL = "FROM " + tstDACmain.class.getName();
Query q = hibSession.createQuery(HQL);
List res = q.list();

Full stack trace of any exception that occurs:
No exception occurs

Name and version of the database you are using:
It doesn'matter.

The generated SQL (show_sql=true):
Is in the message above.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 10:58 am 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
Is this a bug?
Should I put into JRA?
Let me know, please.
Alessandro


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 11:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Lazy fetching is not implemented for -to-one property-ref associations.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 12:16 pm 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
gavin wrote:
Lazy fetching is not implemented for -to-one property-ref associations.


Is it planned to implement it someday?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 9:24 am 
Newbie

Joined: Tue Jun 28, 2005 9:07 am
Posts: 3
I'm also needing badly this functionality - I think I could invest some of my coding time to do it, but I'm still pretty new to Hibernate (I've been debug-stepping over it several times, but still don't have a real grasp of the nuts and bolts). Could I give a hand with it? Does somebody have already an idea how this could be accomplished? Are there any resources available (besides the code itself, of course) where one could learn Hibernate implementation details?

José A. Romero L.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 11:20 am 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
arizzi wrote:
gavin wrote:
Lazy fetching is not implemented for -to-one property-ref associations.


I know I've already asked by at now this problem is growing. The application which I'm working at is suffering for this.

I really need to know if it's planned to implement Lazy fetching for property-ref someday or not.

Otherwise I will have to find a workaround for this (we already have houdred of query on those tables)

Regards
Alessandro Rizzi


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 11:39 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You could also implement it and supply a feature patch to Hibernate.


Top
 Profile  
 
 Post subject: Property-ref and lazy loading
PostPosted: Mon Jan 09, 2006 8:28 pm 
Newbie

Joined: Mon Jan 09, 2006 7:48 pm
Posts: 2
I spent several days on the similar problem. When I do something like this:
HDL = "FROM tstDACmain as main join fetch main.externStatic"
List list session.createQuery(HDL);
hibernate generates right sql statement: one sql statment. But after binding all the data, it throws HibernateException: instance... altered.

Right now I am going to try a workaround:
1. remove property-ref mapping, e.g externStatic
2. do something like HDL = "Select tstDACmain as main, ExternStatic es where main.##=es.##"
3. processing the result.

Any better suggestion?

Thanks


Top
 Profile  
 
 Post subject: Re: Property-ref and lazy loading
PostPosted: Tue Jan 10, 2006 4:24 am 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
julie_yaoy wrote:
I spent several days on the similar problem. When I do something like this:
HDL = "FROM tstDACmain as main join fetch main.externStatic"
List list session.createQuery(HDL);
hibernate generates right sql statement: one sql statment. But after binding all the data, it throws HibernateException: instance... altered.

Right now I am going to try a workaround:
1. remove property-ref mapping, e.g externStatic
2. do something like HDL = "Select tstDACmain as main, ExternStatic es where main.##=es.##"
3. processing the result.

Any better suggestion?

Thanks


To avoid the Hibernate exception try to add the SELECT part of the query. I've seen that many queries written for Hibernate 2.6 without the SELECT part, do not work well with Hibernate 3.
The suggestion you propose do work for INNER join, bu cannot for OUTER join ... so it's not my solution.
By thw way, the query you wrote, works? For what I know, it should not, since you remove the "externStatic" mapping ... it can work it you write the class name of externStatic attribute. I'm I wrong?
Reagards
Alessandro


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 3:29 pm 
Newbie

Joined: Mon Jan 09, 2006 7:48 pm
Posts: 2
Yes, you're right join fetch works for inner join. I've got my problems solved. The exception I got was an unrelated problem of coding.

Regards,
Julie


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