Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Named query to override eager fetching

 
Michael Freake
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm using Hibernate 3.5 and wondering if it is possible to override the eager loading specified using annotations on a class via a named query?

For example, using the classes below I'd like to use a named query to get a particular course version, it's related course and none of the courses collections.

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is.
 
Michael Freake
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. It would be very useful if you could provide an example using the above classes so that anyone else who visits this post, including myself, can benefit from it.

Thanks again.
 
Michael Freake
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I found this: http://ajava.org/online/hibernate3api/org/hibernate/FetchMode.html#SELECT

However, the LAZY fetch mode is now replaced by SELECT, which specifically states that it will perform eager loading... seems like they replaced lazy loading with the exact opposite to me, but I could be wrong.
 
Michael Freake
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, it appears Paul's comment above is incorrect. After reading some documentation and other posts on similar topics, you can use a named query to cause EAGER fetching, but not to cause LAZY fetching.

So basically, my lesson is make everything lazy and use named queries when you need things fetched eagerly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic