-->
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.  [ 4 posts ] 
Author Message
 Post subject: Does HQL ,Criteria Query checks Session Cache
PostPosted: Tue Feb 17, 2009 10:21 am 
Newbie

Joined: Fri Aug 31, 2007 11:58 am
Posts: 19
I tried to search this on forum but was unsuccesful so posting as new question.
I know SQL query always get fresh result from Db rather than checking for dirty objects in Session.What about HQL query and Criteria Query.

_________________
Note:Don't forget to rate,if useful.

Blog:http://elope.wordpress.com/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 10:39 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Criteria and HQL query also use session cache, but they DO a query on the database. Before hydrating the resultset to the fetched data it is checked, whether these objects have already be loaded. This is important to achieve "==" equality within a session:
Code:
        Customer customer = session.get(Customer.class, 1L);

        Query q = session.createQuery("from Customer c where c.id = :id");
        q.setParameter("id", 1L);
       
        //true
        System.out.println(customer == q.uniqueResult());
        //true
        System.out.println(customer == session.createCriteria(Customer.class).add(Restrictions.idEq(1L)).uniqueResult());


Also notice that the session is flushed before querying, so that there would be no dirty objects in a session.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 10:48 am 
Newbie

Joined: Fri Aug 31, 2007 11:58 am
Posts: 19
If that's is the case then i am getting unexpected result.
I am doing following steps
a) Creating a Hibernate enitity and Saving it.
b) Modifying it.
c) Fetch it on the basis of id by Criteria query.

Its giving me the object in Db rather than modified object.If i do commit before fetching then i get modified object.This behaviour has confused me about how Criteria query behave.

_________________
Note:Don't forget to rate,if useful.

Blog:http://elope.wordpress.com/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 11:18 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
What do you mean with: "If i do commit before fetching then i get modified object"? I mean, in both cases your modified object is your "db-objects", because if you do not commit explicitly the session is flushed anyway.

How do you determine which object you get? Could you post you testcode?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.