-->
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.  [ 6 posts ] 
Author Message
 Post subject: OneToMany fetch optimization
PostPosted: Wed May 02, 2007 3:03 am 
Beginner
Beginner

Joined: Sat Apr 07, 2007 4:42 pm
Posts: 24
Hi,

I have a OneToMany relation between Foo and Bar.

class Foo {
@OneToMany(FetchType=Eager)
Set<Bar> bars;
}

The db mapping uses an extra join table.

When I do a "select * from Foo", I get for each row in Foo a join-select on bar and the join table.

Is it possible to optimize it in a way, that only a single select is used?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 5:39 am 
Regular
Regular

Joined: Thu Dec 22, 2005 7:47 am
Posts: 62
Location: Czech Republic
there are some posts on this in this forum.

what are the mappings for the Bar class? what is the sql generated once you issue "select b,f from Bar as b inner join b.foos as f"?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 5:46 am 
Beginner
Beginner

Joined: Sat Apr 07, 2007 4:42 pm
Posts: 24
Could you be so kind to give me some links. Searching "onetomany join" leads to too many hits.

Foo in in Table FOO, Bar in Table BAR, the join table is FOO_BAR.
No further mappings, only the @OneToMany annotation with fetch=EAGER.

About the join: I have to try it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 6:10 am 
Regular
Regular

Joined: Thu Dec 22, 2005 7:47 am
Posts: 62
Location: Czech Republic
One of mine :-)

* http://forum.hibernate.org/viewtopic.php?t=972526&highlight=

Try to search for:

"n+1 problem", "left join fetch" and also you me look into the manual http://www.hibernate.org/hib_docs/v3/reference/en/html/performance.html#performance-fetching

m.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 6:36 am 
Beginner
Beginner

Joined: Sat Apr 07, 2007 4:42 pm
Posts: 24
Ah I see, a fetch join would do it.

I'm using JPA with Hibernate 3 as ORM.

Is the only way to use a fetch join a rewrite the JPA QL statement?
Or can I configure it somewhere (orm.xml)?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 6:44 am 
Regular
Regular

Joined: Thu Dec 22, 2005 7:47 am
Posts: 62
Location: Czech Republic
no JPA QL experience here (just learning), but general recommendation is (hibernate manual) -- use lazy fetching and enforce the eager fetching only selectively (when you need it).

in hbm.xml mapping files you can enforce the eager fetching using the fetch attribute.

also -- do some testing to be sure hibernate issues only the queries you think it does.


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