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
|