-->
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.  [ 2 posts ] 
Author Message
 Post subject: Why eager fetch?
PostPosted: Wed Mar 26, 2008 6:41 am 
Newbie

Joined: Mon Mar 17, 2008 7:46 am
Posts: 3
Location: Oxford University Computing Laboratory
Dear Hibernate users,

I have a conceptual question regarding the eager fetch. In the tutorial example, I noticed that within a session transaction, I can write either

Person aPerson = (Person) session.load(Person.class, personId)

or

Person aPerson = (Person) session
.createQuery("select p from Person p left join fetch p.events where
p.id = :pid")
.setParameter("pid", personId)
.uniqueResult();

The later one is mentioned as eager fetch. I wonder what is the difference between the two approaches of fetching the person object, given its id?

Any clarification or explanation is deeply appreciated.
Jackie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 26, 2008 9:20 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

1. Person aPerson = (Person) session.load(Person.class, personId)
Will only return back an uninitialized proxy. There will be not database hit. The select statement will be only issued when you call a getter on the aPerson.

2. Person aPerson = (Person) session.get(Person.class, personId)
Will issue a select statement directly.

3. Person aPerson = (Person) session.createQuery("select p from Person p left join fetch p.events where p.id = :pid").setParameter("pid", personId) .uniqueResult();

Will issue a select statement to load the Person and the Set of event. The keyword 'fetch' mention that the associated are really loaded and are not proxied (if you have specified lazy="true" in hbm).

Rgds


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