-->
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: is there any difference between Criteria and HQL for query..
PostPosted: Sun Aug 21, 2005 9:39 pm 
Newbie

Joined: Wed Jul 27, 2005 11:13 pm
Posts: 10
is there any difference between Criteria and HQL for query an object?

Hibernate version:3.0

Mapping documents:
Code:
<class deviceInfo>
...
      <many-to-one name="deviceType"  column="device_type" class="DeviceType" fetch="join" not-null="true"/>
...



Code between sessionFactory.openSession() and session.close():
Criteria:
Code:
            resultList = session.createCriteria((DeviceInfo.class))
                                .list();


HQL:
Code:
            Query query = session.createQuery("from DeviceInfo");
            resultList = query.list();




Doubt is:
after close the session, I can get infomation of DeviceType(class, may to one in DeviceInfo) when I use Creteria, but I can't get the information of DeviceType when I use HQL.
How can I get the same result, thanks....


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 21, 2005 11:53 pm 
Expert
Expert

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

Look closely in to the Query generated because of Criteria?
You could see a select for the child table too.

Since you have set the LAZY="true" in mapping you get all the values in Child Table when you use Criteria.

If you want to use HQL then you have explicitly call the child too.

I will give +1 to Criteria than HQL.

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 12:39 am 
Newbie

Joined: Wed Jul 27, 2005 11:13 pm
Posts: 10
ramnath,
I mean the conditions is same to Criteria and HQL query,
including lazy setting.

I can get all object values via HQL except to call the child?

thanks a lot.

William


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 2:35 am 
Newbie

Joined: Tue Aug 16, 2005 8:28 am
Posts: 15
Ramnath,

In HQL your associations have to be specified in your HQL, otherwise they will not get loaded.

Read up "Hibernate ignores my outer-join="true" setting and fetches an association lazily, using n+1 selects!"

at

http://www.hibernate.org/117.html#A18

_________________
Give Credit Where Due (if I answer your post)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 10:26 pm 
Newbie

Joined: Wed Jul 27, 2005 11:13 pm
Posts: 10
hi,
Except using left join fetch, how can we get child table object?
call the child property? it not seem good method...


regards,
William


Top
 Profile  
 
 Post subject: I got it...
PostPosted: Tue Aug 23, 2005 4:51 am 
Newbie

Joined: Wed Jul 27, 2005 11:13 pm
Posts: 10
I got it,

AS pearl8 said, ref: http://www.hibernate.org/117.html#A18

HQL

Code:
            Query query = session.createQuery("from DeviceInfo dev [b]left join fetch dev.deviceType[/b]");
            resultList = query.list();



but the HQL can't run in Hibernate3.0, have to update it to 3.1 later....


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.