-->
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: many-to-one lazy loading not working if property-ref is set
PostPosted: Fri Feb 20, 2004 12:02 pm 
Newbie

Joined: Fri Feb 20, 2004 10:59 am
Posts: 13
Location: Italy
Hi all,

I have a class "Ruolo" many-to-one mapped to the class "TipoRuolo".
The mapping file is:

[...]
<class name="Ruolo" table="DBO_T_RUOLO">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="codiceProdotto" column="CPRODOTTO" type="string"/>
<property name="codiceContratto" column="CCTR" type="string"/>
<property name="codiceCliente" column="CCLI" type="string"/>
<many-to-one name="tipoRuolo" column="CTIPORUOLO" class="TipoRuolo" property-ref="codiceTipoRuolo"/>
</class>

<class name="TipoRuolo" table="DBO_T_TIPORUOLO" proxy="TipoRuolo">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="codiceTipoRuolo" column="CTIPORUOLO" type="string"/>
<property name="descrizioneTipoRuolo" column="DTIPORUOLO" type="string"/>
</class>
[...]

As you can see lazy loading should be enabled, having set the "proxy" attribute. However if I run the simple query:

Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
List ruoli = session.find("from Ruolo r where r.codiceCliente='325831'");
for (Iterator it = ruoli.iterator(); it.hasNext();) {
Ruolo r = (Ruolo) it.next();
out.println("\tcodice cliente: " + r.getCodiceCliente());
}
tx.commit();

the query does not reference the tipoRuolo property, nevertheless hibernates generates SQL queries also for TipoRuolo (non-lazy behaviour). This is the log file:

Hibernate: select ruolo0_.id as id, ruolo0_.CPRODOTTO as CPRODOTTO, ruolo0_.CCTR as CCTR, ruolo0_.CCLI as CCLI, ruolo0_.CTIPORUOLO as CTIPORUOLO from DBO_T_RUOLO ruolo0_ where (ruolo0_.CCLI='325831' )
Hibernate: select tiporuolo0_.id as id0_, tiporuolo0_.CTIPORUOLO as CTIPORUOLO0_, tiporuolo0_.DTIPORUOLO as DTIPORUOLO0_ from DBO_T_TIPORUOLO tiporuolo0_ where tiporuolo0_.CTIPORUOLO=?
Hibernate: select tiporuolo0_.id as id0_, tiporuolo0_.CTIPORUOLO as CTIPORUOLO0_, tiporuolo0_.DTIPORUOLO as DTIPORUOLO0_ from DBO_T_TIPORUOLO tiporuolo0_ where tiporuolo0_.CTIPORUOLO=?
Hibernate: select tiporuolo0_.id as id0_, tiporuolo0_.CTIPORUOLO as CTIPORUOLO0_, tiporuolo0_.DTIPORUOLO as DTIPORUOLO0_ from ........

I suppose that the non-lazy behaviour is somehow related to the "property-ref" attribute. Infact I modified the mapping file as follows:

[...]
<class name="Ruolo" table="DBO_T_RUOLO">
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<property name="codiceProdotto" column="CPRODOTTO" type="string"/>
<property name="codiceContratto" column="CCTR" type="string"/>
<property name="codiceCliente" column="CCLI" type="string"/>
<many-to-one name="tipoRuolo" column="CTIPORUOLO" class="TipoRuolo"/>
</class>

<class name="TipoRuolo" table="DBO_T_TIPORUOLO" proxy="TipoRuolo">
<id name="codiceTipoRuolo" column="CTIPORUOLO" type="string">
<generator class="native"/>
</id>
<property name="descrizioneTipoRuolo" column="DTIPORUOLO" type="string"/>
</class>
[...]

hence I eliminated the property-ref attribute and set codiceTipoRuolo as primary key on the TipoRuolo class. Without changing anything in the java code and running the same query I got a lazy load of the Ruolo class (i.e. no SQL queries on the DBO_T_TIPORUOLO table).

So it seems to me that setting the "property-ref" attribute prevents from lazy loading of a class. Is it a malfunction or am I missing something?

Thanks.

Dario.

P.S.: I'm working with Hibernate 2.1.2 + MySql 4.0.17


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 12:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes it prevents it.
Proxied objects are identified by their ids, but we don't know it when loading Ruolo (we only have codiceTipoRuolo).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 12:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
And it cannot be fixed wo breaking the golden rule 1 instance per id for 1 session.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 12:30 pm 
Newbie

Joined: Fri Feb 20, 2004 10:59 am
Posts: 13
Location: Italy
Emmanuel,

thanks for your prompt and clear answer.

Best regards.

Dario.


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.