-->
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: Query vs Criteria
PostPosted: Thu Jan 25, 2007 7:40 am 
Newbie

Joined: Thu Jan 25, 2007 5:31 am
Posts: 8
I was reading in the hibernate documentation about Query and Criteria
but I couldn't find which is better for what
the doc says that if you prefer to use object oriented method for querying db instead of building string query you can use Criteria
I'm asking:
which is better for what ?
and which is faster for what ?
and which is best practice and so on...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 8:44 am 
Newbie

Joined: Tue Dec 12, 2006 3:19 am
Posts: 17
Location: Spain
Hello,
(first of all, sorry about my english)

I started using HQL Query. From my point of view, if you have an idea about SQL it's easier to devolop some applications than using Criteria. However, I found some cases where Criteria makes life easier.

For instance, If you want to add restrictions to a query dinamically. I think it's easier to use Criteria. Imagine that you should create a query which depends on the restrictions a user selected on your front-end.

Imaging user want to obtain information using restriction 1:
Code:
        createCriteria c
        If (user want restriction 1)
          c.add(Restrictions 1)
        If (user want restriction 2)
          c.add(Restrictions 2)
        c.list


When using HQL you have to code the restriction handly, and you have to control several aspect tha Criteria do

Code:
       query=" where "
        If(user want restriction 1)
           query = query + "restriction 1 ..."
        If(user want restriction 2)
           query = query + "restriction 2 ..."

        createQuery q ("select * from "+query).list


I hope help you :-S


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 8:54 am 
Newbie

Joined: Thu Jan 25, 2007 5:31 am
Posts: 8
OK, so which is faster ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 9:42 am 
Newbie

Joined: Tue Dec 12, 2006 3:19 am
Posts: 17
Location: Spain
Soulstealer wrote:
OK, so which is faster ?

Boht Criteria and HQL should translate to SQL language.
So, as far as I know SQL (createSQLQuery()) should be the fastest method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 10:12 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I try to use Query in all cases as it's just easier to write b/c hql is so similar to sql. The only time I use Criteria is when I need a dynamically built query from user input, need to perform a transformation, or need to override some lazy settings.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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.