-->
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: HQL Update/Delete Does Not Work the Same Way?
PostPosted: Wed Dec 10, 2008 2:23 am 
Newbie

Joined: Wed Dec 10, 2008 12:03 am
Posts: 4
I have 2 scenarios:
1. Create a new object, persist it. Then update it using HQL. Query it using HQL by primary key. The retrieved object is not changed.
2. Create a new object, persist it. Then delete it using HQL. Query it using HQL by primary key. Nothing retrieved - the object is deleted.

The question is:
1. Does HQL talk to hibernate cache or DB directly?
2. Why update/delete with HQL have different behavior?


Thanks & Best Regards,

Hudson


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 4:28 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
1. Does HQL talk to hibernate cache or DB directly?


DB. Second-level cache regions that are affected will be cleared. First-level cache of sessions are not affected. Loaded entities are not affected. If you want to update an existing object use Session.refresh().


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 2:02 am 
Newbie

Joined: Wed Dec 10, 2008 12:03 am
Posts: 4
I'm sorry for replying late. What's the difference between first level cache and second level cache? Why update and remove have different behavior to cache?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 2:02 am 
Newbie

Joined: Wed Dec 10, 2008 12:03 am
Posts: 4
I'm sorry for replying late. What's the difference between first level cache and second level cache? Why update and remove have different behavior to cache?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 4:49 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The first-level cache is tied to a single Session. If you have multiple sessions then you have multiple first-level caches as well. When a session dies, so does the first-level cache attached to it. The first level cache stores complete entities.The first-level cache grows as you load entities with the session. It doesn't have a limit on the number of objects and it doesn't shrink automatically.

The seconds-level cache is a shared cache and lives with the SessionFactory. The second-level cache doesn't store complete entities, but only the values that make up the entities. The second-level cache can be configured to store a maximum number of entities and can automatically throw away old information. Some implementations can store the cache on disk. The second-level cache is optional and can be configured separately for each entity type and collection.

Hibernate is primarily an object/relational mapping framework. It is expected that manipulation of objects is done in memory. But Hibernate also provides support for manipulation directly in the database with UPDATE/DELETE/INSERT statments, but this will not affect in-memory state of entities. The second-level cache is cleared of values that may have been affected by the SQL statement. It is not very fine-grained and results in a database hit the next time the values are requested.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 24, 2009 3:21 am 
Newbie

Joined: Wed Dec 10, 2008 12:03 am
Posts: 4
Thank you for your explanation! The Update and Delete have different behavior is still weird.


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.