-->
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: Too many selects for just retrieving a single object.
PostPosted: Sun Aug 24, 2008 8:39 am 
Newbie

Joined: Tue Jun 10, 2008 3:51 am
Posts: 10
When retrieving entity, hibernate first selects all primitive fields and ids for non-primitive fields in a select statement, then starting to select non-primitive fields using other statements. The problem is, if Class A has 100 non-primitive fields, then they will at least 100 statements used for only retriving a single A instance. Is there any way to optimize it to few selects?

I know fetch strategies could be configured, but after reading related document, still cannot figure out how to optimize it.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 24, 2008 4:13 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The most important thing is to make sure that proxies are enabled for all classes. This is done on a class-by-class basis with <class ... lazy="true">.

If you want to override this for some associations you can specify <many-to-one ... fetch="join">. Hibernate will use a single query with join statements to also load the associated objects.

You may also consider using the second-level cache.

A lot of information about this can be found in the Hibernate documentation: http://www.hibernate.org/hib_docs/v3/re ... rmance.htm

It is kind of hard to answer questions about optimization without knowing anything about the data model, use cases, etc. Optimizing one use case may turn out to be very bad for something else. It is very often a matter of trying to find an acceptable average performance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2008 3:24 am 
Newbie

Joined: Tue Jun 10, 2008 3:51 am
Posts: 10
I've configured second-level cache. But it always miss hiting. Everytime I find the same entity, HB always logs the following log. I guess that means the entity is not loaded from cache.

2008-08-26 15:18:36,859 INFO [STDOUT] Hibernate: /* select m from Medium m */ select medium0_.medium_id as medium1_16_, medium0_.medium_desc as medium2_16_ from medium_lu medium0_

...

2008-08-26 15:18:40,375 DEBUG [org.hibernate.engine.TwoPhaseLoad] adding entity to second-level cache: [com.my.Medium#8]
2008-08-26 15:18:40,375 DEBUG [org.hibernate.cache.ReadOnlyCache] Caching: com.my.Medium#8
2008-08-26 15:18:40,375 DEBUG [org.hibernate.engine.TwoPhaseLoad] done materializing entity [com.my.Medium#8]


Top
 Profile  
 
 Post subject: Re: Too many selects for just retrieving a single object.
PostPosted: Tue Aug 26, 2008 4:23 am 
Newbie

Joined: Thu Jun 19, 2008 4:49 am
Posts: 12
look down


Last edited by jklfdskj on Tue Aug 26, 2008 4:24 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Too many selects for just retrieving a single object.
PostPosted: Tue Aug 26, 2008 4:23 am 
Newbie

Joined: Thu Jun 19, 2008 4:49 am
Posts: 12
superzz wrote:
Is there any way to optimize it to few selects?


how do you retrieve your entity? hql? try "left join fetch" then.

for example, if your class a has class b and class c, you could fetch all data at once by querying

from class a left join fetch a.b left join fetch a.c


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.