aspose file tools*
The moose likes Object Relational Mapping and the fly likes Suppressing association retrievals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Suppressing association retrievals" Watch "Suppressing association retrievals" New topic
Author

Suppressing association retrievals

Tony Crisera
Greenhorn

Joined: Nov 09, 2007
Posts: 8
What I am seeing is that when I issue an HQL query intended to retrieve a filtered list of a single object, it is immediatley followed by individual selects to retrieve an associated object for each item returned by the original query. I've been reading through Hibernate in Action regarding mapppings and HQL queries and have not been able to figure out how to suppress the additional selects being issued.

For class GiftCode, I have this bi-directional one-to-one association defined, (recently adding the outer-join attribute)

<one-to-one name="giftCredit"
class="GiftCredit"
outer-join="true"
property-ref="giftCode"/>

When I run this simple query:
select giftCode from GiftCode giftCode
where giftCode.redeemed = 'N'
and giftCode.expirationDate < ?

I may get back 5 GiftCode objects, but then 5 selects are issued for GiftCredits. I realize I could add a fetch join, but I do not need those GiftCredits returned. Is there any way to suppress these additional selects?

Thanks, TC
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Which version of Hibernate are you using? By default Hibernate has lazy loading on, did you change that anywhere? you can remove the outer-join="true" and you can put lazy="true" in the relation mapping, but like I said lazy="true" is the default.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Tony Crisera
Greenhorn

Joined: Nov 09, 2007
Posts: 8
We are using Hibernate 3.2. It seems Hibernate in Action was written prior to ver 3. Were there changes from what is indicated in that book as far as fetching strategies and default behavior?

Thank, TC
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

In 3.2 Lazy is the default. So remove outerjoins="true" and lets see your entire mapping, because somewhere must be set to turn that off, or in your code you are getting the parent and either looping through the collection, or maybe just calling size() on the collection, which could cause Hibernate to load the collection.

Mark
 
jQuery in Action, 2nd edition
 
subject: Suppressing association retrievals