-->
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: Multiple sql query for simple hql query.
PostPosted: Tue Jul 27, 2004 9:21 am 
Newbie

Joined: Thu Mar 18, 2004 3:56 am
Posts: 13
Hi to all,

I use the following hql query:
--------------------------
from ValoreDato valoreDato
--------------------------
and, on my surprise, I've noticed that Hibernate use an N + 1 sql query while retriving persisted entity:
In the first, it load only the IDs of entitys (idValoreDato), and then, for every id, it make a single sql query (so, if i have 1000 ValoreDato in DB, i get 1001 sql query).

This happen also if i put a where that make a condition on the id of the entity:
--------------------------
from ValoreDato valoreDato
where valoreDato.idValoreDato = 1
--------------------------
I get one query for load all id that are equal to 1, and than, i get another query to load the persisted object.

How can i tell to Hibernate to load all object in a single query, instad using one query for each object?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 9:31 am 
Regular
Regular

Joined: Tue Oct 28, 2003 8:25 am
Posts: 72
Location: Belgium
Use Session.find() instead of Session.iterate()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 9:41 am 
Newbie

Joined: Thu Mar 18, 2004 3:56 am
Posts: 13
lorban wrote:
Use Session.find() instead of Session.iterate()


Ok, but if i need to use session.createQuery()?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 9:47 am 
Regular
Regular

Joined: Tue Oct 28, 2003 8:25 am
Posts: 72
Location: Belgium
Then use Query.list() instead of Query.iterate().

You should also bookmark this: http://www.hibernate.org/hib_docs/api/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 10:21 am 
Newbie

Joined: Thu Mar 18, 2004 3:56 am
Posts: 13
Thanks.

Only another question:
How can i specify to Hibernate to load one (or more) many-to-one association in a single sql query?

I've tried using join in hql, but seem that it no work, and i've the "max_fetch_depth" setted to "3".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 11:00 am 
Newbie

Joined: Thu Mar 18, 2004 3:56 am
Posts: 13
Never mind...
Seem that, for make load many-to-one associations, i need to omit the select clause (i usually select the root entity), and specify the various join in the form clause.


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.