• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JQL and eager

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I have the following relation:


I have the following query:


When i do the query, i want to override the mapping (eager/lazy), but what I see is that it run one select for all user and for each user it run individual select to get the address.
In this case the relation is eager (I am working with JPA above Hibernate and OneToOne default is eager) , so when it load User it also load Address and it does it for each user.
I want in query to get the control on the fetching; I don�t want to bring objects, which weren�t defined explicitly in the query.

Is there a way to do this?

Thank you
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the Query object api has a setFetchMode() method.

The wisdom says map all relationships as lazy.

Eager is basically, when there is one query run against the database.

In your description sounds like the one-to-one is loading lazily. one query against the database to get the UserEntity, and then another second query to get the Address, that says Lazy to me.

Good Luck

Mark
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code:


As you can see i dont run on the objects, just execute the query.
In the SQL i see that i see that it load address, so its means it treat the realtion as eager and fetch address althought i didnt write it in the query.

I want the fetching to be eager when i do findById or get, but when i run query i want the query to win.

Look on the following article:
http://blogs.jboss.com/blog/acoliver/2006/01/23/Hibernate_EJB3_Tuning.txt in the fetch strategies section he wrote:
In H3 when you run an HQL query the mapping file's fetch strategy is ignored for each association and they are loaded lazily by default
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by avihai marchiano:
Here is my code:




In many to one and one to one eager is the default.

In your code about, if you want to set the fetch mode you can call

qu.setFetchMode(), that is all that I was bringing up.

Mark
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no setFetchMode api for query.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry, I was thinking the Criteria has the setFetchMode.

In HQL there are keywords to say what you want loaded eagerly. That is used in the Query object.

You can map your one to one to be "extra lazy" in the mapping to turn off the default eager of the one to one, if you don't want the Address to load.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic