-->
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.  [ 8 posts ] 
Author Message
 Post subject: Fetch strategy seemingly ignored
PostPosted: Wed Nov 16, 2005 11:38 am 
Newbie

Joined: Wed Nov 16, 2005 11:20 am
Posts: 11
I'm tearing my hair out at the following problem.
I have a database of products. each product links to an imageSet object which I DO NOT want to be loaded unless I explicitly call getImages(). Hibernate3 claims to use lazy (proxy) fetching by default for associations, and I have even set lazy="true" for both the child class and the association but to no avail. Calling list() on a query for all products initializes the images too (causing a massive database hit).

Am I doing something stupid? Have I missed a setting?
I have grepped for any fetch or lazy settings just to check I hadn't changed any defaults, and I haven't.

Hibernate version:
3.0.5

Mapping documents:
<class name="org.trapdoor.bedsdirect.client.entities.Product">
<id name="id">
<column name="id"/>
<generator class="identity"/>
</id>
<version type="long" unsaved-value="undefined" name="version"/>
...
<many-to-one unique="true" lazy="true" name="images">
<column name="imageset"/>
</many-to-one>
<many-to-one unique="true" lazy="true" name="hoverImages">
<column name="hoverimageset"/>
</many-to-one>
...
</class>

<class lazy="true" name="org.trapdoor.bedsdirect.client.entities.ImageSet">
<id name="id">
<column name="id"/>
<generator class="identity"/>
</id>
<version type="long" unsaved-value="undefined" name="version"/>
<component name="small">
<property name="mimeType" column="small_mimeType"/>
<property name="base64Data" type="text" column="small_base64Data"/>
</component>
<component name="medium">
<property name="mimeType" column="medium_mimeType"/>
<property name="base64Data" type="text" column="medium_base64Data"/>
</component>
<component name="large">
<property name="mimeType" column="large_mimeType"/>
<property name="base64Data" type="text" column="large_base64Data"/>
</component>
</class>

Code between sessionFactory.openSession() and session.close():
s.createQuery("select p from Product p").list();

Name and version of the database you are using:
MySql 4.1.12

The generated SQL (show_sql=true):
14:58:39,423 DEBUG SQL:324 - select product0_.id as id, product0_.version as version19_, product0_.name as name19_, product0_.shortDescription as shortDes4_19_, product0_.full14:58:41,766 DEBUG SQL:324 - select imageset0_.id as id0_, imageset0_.version as version11_0_, imageset0_.small_mimeType as small3_11_0_, imageset0_.small_base64Data as small41

Debug level Hibernate log excerpt:
...(145 products loaded)...
14:58:41,762 DEBUG Loader:528 - total objects hydrated: 145
14:58:41,763 DEBUG TwoPhaseLoad:96 - resolving associations for [org.trapdoor.bedsdirect.client.entities.Product#1]
14:58:41,764 DEBUG DefaultLoadEventListener:143 - loading entity: [org.trapdoor.bedsdirect.client.entities.ImageSet#1]
14:58:41,764 DEBUG DefaultLoadEventListener:290 - attempting to resolve: [org.trapdoor.bedsdirect.client.entities.ImageSet#1]
14:58:41,765 DEBUG DefaultLoadEventListener:326 - object not resolved in any cache: [org.trapdoor.bedsdirect.client.entities.ImageSet#1]
14:58:41,765 DEBUG BasicEntityPersister:2467 - Materializing entity: [org.trapdoor.bedsdirect.client.entities.ImageSet#1]
14:58:41,765 DEBUG Loader:1340 - loading entity: [org.trapdoor.bedsdirect.client.entities.ImageSet#1]
14:58:41,766 DEBUG AbstractBatcher:290 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
14:58:41,767 DEBUG AbstractBatcher:378 - preparing statement
14:58:41,848 DEBUG LongType:59 - binding '1' to parameter: 1
14:58:42,378 DEBUG AbstractBatcher:306 - about to open ResultSet (open ResultSets: 0, globally: 0)
14:58:42,379 DEBUG Loader:405 - processing result set
14:58:42,379 DEBUG Loader:410 - result set row: 0
14:58:42,380 DEBUG Loader:828 - result row: EntityKey[org.trapdoor.bedsdirect.client.entities.ImageSet#1]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 8:35 am 
Newbie

Joined: Wed Nov 16, 2005 11:20 am
Posts: 11
Well I THINK I've found the problem.
setting lazy=true JUST on the ImageSet (child) class works.
I has the association set to lazy=true as well, and this seems to break it.

Surely this is a hibernate bug?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 8:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
read the doc, many-to-one lazy=true means something different than proxy

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 9:41 am 
Newbie

Joined: Wed Nov 16, 2005 11:20 am
Posts: 11
From the hibernate reference:

Quote:
6.1.10. many-to-one
...
(12) lazy (optional - defaults to proxy): By default, single point associations are proxied. lazy="true" specifies that the property should be fetched lazily when the instance variable is first accessed (requires build-time bytecode instrumentation). lazy="false" specifies that the association will always be eagerly fetched.


If I understand correctly, lazy="true" should still specify lazy fetching, just using bytecode instrumentation rather than proxies. It still shouldn't hit the database.

Also, having "true","false",and "proxy" options is more than a little confusing!

I am now having a similar problem where a collection is being fetched along with the parent.

I have a bidirectional (many-to-many) association, with each end (supposedly) defaulting to lazy (I have also tried setting lazy=true).

doing a "from child" causes a select on the association table for each child returned! Surely this should happen on a per-child basis, when the collection is accessed?

Thanks for your help[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 9:54 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You probably did not enhance your classes, so <many-to-one lazy="true" just cannot work.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 10:04 am 
Newbie

Joined: Wed Nov 16, 2005 11:20 am
Posts: 11
emmanuel wrote:
You probably did not enhance your classes, so <many-to-one lazy="true" just cannot work.


Hmm, not sure what you mean by not enhancing my classes. This is done at runtime by hibernate IIRC. Anyway, the many-to-one lazy="true" does now work, it's the many-to-many I'm having problems with.

Both ends of the bidirectional association are defined as "<set...<many-to-many...", and querying for the parents does NOT initiate a series of selects on the association table. Querying for the children DOES however. I've tried without overriding fetch and lazy settings, and also by overriding fetch="select" lazy="true" and just fetch="select". Nothing seems to make a difference - retrieving the children always hits the database.

Thanks again for your time/help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 10:14 am 
Newbie

Joined: Wed Nov 16, 2005 11:20 am
Posts: 11
Ok, having read the docs more thoroughly:

<set> has lazy and fetch attributes, which default to "true" and "select" respectively, which is what I want.
<many-to-many> has only a fetch attribute which defaults to "join". I don't understand how this works, since now the set and many-to-many defaults conflict.

I set fetch="select" on the many-to-many tag but it makes no difference (and the SQL was showing multiple selects, not joins anyway.

Why isn't the collection being lazily fetched?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 10:37 am 
Newbie

Joined: Wed Nov 16, 2005 11:20 am
Posts: 11
Oops, slightly embarassing. I had a debugging method hidden away that accessed the size of the collection. Sorry!


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