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.  [ 2 posts ] 
Author Message
 Post subject: Reloading object from DB without overwriting Hibernate cache
PostPosted: Mon Aug 25, 2008 10:17 pm 
Newbie

Joined: Mon Aug 25, 2008 10:07 pm
Posts: 1
Dear Hibernate experts.

I have the following problem.

My method A needs to check the persistent object O (instance O1) and see if it has changed since it was last persisted in the DB.
In order to do that A needs to reload a "fresh" version of O (instance O2 of the same object) and compare it with O1.

However if I use the Session.Refresh(), O1 gets reinitialized with the same values as O2 and I cannot compare the 2 objects anymore.
And if I use Session.Get(), then O2 will be the same as O1 because Hibernate will use the cached version of O.

Is there any way to reload O from the DB without overwriting the cache (i.e. O1) ?

I guess a solution would be to load O2 in a another session but that sounds pretty heavy for this simple problem.

Any suggestion would be greatly appreciated.

Thank you.

Clement


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2008 1:07 am 
Newbie

Joined: Sun Aug 31, 2008 9:22 pm
Posts: 2
Hi Clement,

You can use a criteria to obtain an object from the database.
You can write something like that :

Code:
dim O2 as YourObjectType
Using session As ISession = _SessionFactory.OpenSession()
    Dim c As ICriteria = session.CreateCriteria(GetType(YourObjectType)) _
                    .Add(Expression.Eq("PropertyX", O1.PropertyX)) _
                    .Add(Expression.Eq("PropertyY", O1.PropertyY)) _
                    .SetMaxResults(1)
    O2 = c.UniqueResult(Of YourObjectType)()
End Using



Hope this will help you.

_________________
Gérard


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.