Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Suppressing association retrievals

 
Tony Crisera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Crisera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic