-->
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: Does the Criteria API use PreparedStatements by default?
PostPosted: Fri Dec 23, 2005 10:07 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Does the Criteria API use PreparedStatements by default?

I've looked through the manual, but couldn't find any information about criteria vs PreparedStatements.
I currently create a DetachedCriteria on every DAO call, because I need to set a parameter on it:

Code:
    public List<A> loadAll(String currentName) {
        DetachedCriteria loadAllCriteria = DetachedCriteria.forClass(aClass);
        loadAllCriteria.add(Restrictions.eq("name", currentName));
        return getHibernateTemplate().findByCriteria(loadAllCriteria);
    }


aClass I know at construction time (not at compile time) so I should be able to prepare it at contstruction time (I doens't change between method invokations).
currentName changes between method invokations.

It's hard (impossible?) to write a prepared query because I don't know aClass at compile time.

Is the code above using a prepared statement?
Is there any way to reuse DetachedCriteria 's and still use a parameter?

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 4:49 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Is there some documentation anywhere about Criteria usage of PreparedStatements?

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 5:21 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hibernate also offers an object-oriented query by criteria API that can be used to formulate type-safe queries. Hibernate of course uses PreparedStatements and parameter binding for all SQL communication with the database. You may also use Hibernate's direct SQL query feature or get a plain JDBC connection from a Session in rare cases.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 8:40 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Thanks for the clarification :)
Where did you get that statement?

In my case, if I used it for 3 classes: Person, Dog, Cat and 8 method calls (each with a different currentName parameter value) then how many prepared statements would it create? (I hope 3.)

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 1:34 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Here is the link
http://www.hibernate.org/hib_docs/refer ... start.html

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 6:33 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Thanks, too bad it doesn't state at which level criteria uses preparedstatements.

I am stilling wondering for my case, if I used it for 3 classes: Person, Dog, Cat and 8 method calls (each with a different currentName parameter value) then how many prepared statements would it create?

The code I used for my case is above.

Is there anyway to log that hibernate creates or reuses a preparedstatement? Normal show_sql log isn't clear enough.

_________________
http://www.ohloh.net/accounts/ge0ffrey


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.