Hibernate lazy loading does not work with many cards

I have a performance issue in my multidimensional mapping. When I debug an SQL query in a log file, the main query is fine, but after I have another query representing a multi-valued mapping of objects.

Entity.hbm.xml:

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <class name="com.omb.database.mapping.MyEntity" table="MY_ENTITY"> <id name="id" type="java.lang.Integer"> <column name="ENTITY_ID"/> <generator class="sequence"> <param name="sequence">SEQ_MY_ENTITY</param> </generator> </id> <property name="prop1" type="string" column="PROP1" /> <many-to-one name="object1" column="OBJECT1_ID" class="com.omb.database.mapping.Object1" /> <many-to-one name="object2" column="OBJECT2_ID" class="com.omb.database.mapping.Object2" /> </class> </hibernate-mapping> 

Object1.hbm.xml:

 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping default-lazy="true"> <class name="com.omb.database.mapping.Object1" table="TABLE_OBJECT_1"> <id name="id" type="java.lang.Integer" column="OBJECT1_ID" /> <property name="label" type="string" column="LABEL_OBJECT_1" length="15" /> </class> </hibernate-mapping> 

Object2.hbm.xml:

 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping default-lazy="true"> <class name="com.omb.database.mapping.Object2" table="TABLE_OBJECT_2"> <id name="id" type="java.lang.Integer" column="OBJECT2_ID" /> <property name="label" type="string" column="LABEL_OBJECT_2" length="15" /> </class> </hibernate-mapping> 

HBM Request:

 public List<Entity> findByObject1Id(Integer object1Id) throws DataAccesException { List<Entity> results = null; try { Query query = this.getSession().createQuery( "from Entity ent where ent.object1.id = :object1Id"); query.setParameter("object1Id", object1Id); results = query.list(); } catch (HibernateException hbe) { throw new DataAccesException(hbe); } return results; } 

in pom.xml

 <!-- Hibernate 3 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.2.6.ga</version> <exclusions> <exclusion> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> </exclusion> <exclusion> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </exclusion> <exclusion> <groupId>asm</groupId> <artifactId>asm-attrs</artifactId> </exclusion> </exclusions> </dependency> 
+8
java sql xml mapping hibernate
source share
2 answers

You tried using FetchMode.SELECT, for example:

 <many-to-one name="object1" column="OBJECT1_ID" class="com.omb.database.mapping.Object1" fetch="select" /> 
+2
source share

Your mapping looks ok. As described here 5.1.1. Entity

Lazy <class> attribute defaults to true

  • lazy (optional): lazy fetching can be disabled by setting lazy = "false".

The same for <many-to-one> : 5.1.7.1. Using a foreign key or lazy attribute association table :

  • lazy (optional - default - proxy): single-point associations are merged by default. lazy="no-proxy" indicates that the property should appear lazily when the first instance variable is opened. This requires a build-time bytecode toolkit. lazy="false" indicates that the association will always be looking forward.

So where is the problem?

I would say in your debug window. Since you have a link to your list and you are looking at the result, the link is also loaded at the moment of its execution. Little - but loaded. This is actually what we want. The proxy - on first order - forces the load.

Try to remove it from the watch. Or close the session and then put it in the clock ... You should see that the request used above is not loading links ... only if it is really accessible ... even through the debug window

+3
source share

All Articles