-->
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.  [ 5 posts ] 
Author Message
 Post subject: Fetch join vs Hibernate.initialize()
PostPosted: Wed Mar 07, 2007 11:51 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 11:39 am
Posts: 24
I have an Item class that has a collection bids.
By default (Hibernate 3) my pojo is lazy so when I want to getBids() I need first to Hibernate.initialize(item) so as later to have item.getBids();

Correct?

I read in a book that I use another lazy strategy and do something like that:
Criteria criteria = session.createCriteria(Item.class);
criteria.setFetchMode("bids", FetchMode.JOIN);
List allItems = criteria.list();

I can have bids by using a single SQL query.
How exactly does it mean?The above gives me allItems. If I iterate the collection to get bids won't I have again new sql executions??

Which of the 2 ways is the best?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 6:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
If you load the object then Access (which in session) or Hibernate.initialise there will be two queries to the DB.

If you use HQL or Criteria the join fetch the collection then only a single (larger) query the the DB will be required to collect the data.

One query maybe more efficient than two (DB speed, network latency etc).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 08, 2007 4:54 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 11:39 am
Posts: 24
ok I understood but the thing is with join fetch I get all the items correct?
If I go to get the bids won't I have new sql queries?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 3:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
If you 'join fetch' then this will collect all items in the collection so walking the collection will not cause the collection query to be re-issued.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 4:26 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 11:39 am
Posts: 24
ok thank you for your answers...Can I also ask another question? I have my pojo item which is lazy and the collection bids which is also lazy. Shouldn't I have a Hibernate Initialization exception when I do item.getBids()? ..The thing is that I don't have any exception.I thought that in order not to have any exception I should do Hibernate.initialize(item.getBids). Don't we use this anymore in Hibernate 3?


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