-->
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: Question about cache v.s database reads
PostPosted: Thu Mar 29, 2007 11:05 am 
Newbie

Joined: Fri Oct 17, 2003 11:31 am
Posts: 19
What I'm trying to do is to determine what values have been modified, prior to persisting the object to the database (for logging purposes).

The question I have is when does the first level cache get updated with modified data. Here is what I'm doing :

1) I read an object :

Code:
Subject subject1 = HibernateUtil.getEntityManager().find(Subject.class, 1);


2) I change a value on my object :
Code:
    subject1.setFirstName("Bill");


3) Later on, within the same JTA transaction, and before I actually persist / flush my changes, I do another read from the database :

Code:
Subject oldSubject1 = HibernateUtil.getEntityManager().find(Subject.class, 1);


4) I then attempt to determine what has changed between the two versions of this Subject object :
Code:
    log.debug("New Firstname value = " + subject1.getFirstName());
    log.debug("Old  Firstname value = " + oldSubject1.getFirstName());

5) My confusion lies in the fact that both these debugs print 'Bill', when what I expect is the oldSubject1.getFirstName() to return the value from the database.

I was under the impression that my subject1 values wouldn't be persisted to the cache or the database until I do an em.persist(subject1) and a flush.

Is there something I'm missing with regards to when the values are flushed to the first level cache and/or the database? When I do my second read I would expect to get the old value that's in the database, even though I'm still in the same transaction.

Any help / clarification would be greatly appreciated.

I'm using Hibernate 3.2.2 with an oracle 10g database.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 2:17 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Step 1.
----------
Subject object is being read from DB and your first level session is getting populated with this object.

Step 2.
---------
Since you are changing the one attribute in the object, the value present in the session is also getting updated.

Step 3.
----------
This would merge your value from DB with the session and return the same object. So after this step subject1 and oldSubject1 are really pointing to the same object.

Step 4.
----------
Expected behavior.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 2:33 pm 
Newbie

Joined: Fri Oct 17, 2003 11:31 am
Posts: 19
Why / how is the session being updated when I change a value in my Subject object, shouldn't I have to do a flush() or persist() before the session is updated with the new value?

Is it possible to get the old value (pre flush) out of the database for this object, since I need to be able to determine what has changed between the old and the new Subject records?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 2:51 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Session is getting updated because it is the same java object.
flush() doesnt update session, it merges the session value with DB value.

One way (there could be other ways also) to have your pre change values would be like this
1. Read the object.
2. Evict the object from the session. This object will now have the old values.
3. Read the object again to a diff java object.
4 make changes to this new java object.

Perform the comparison among these 2 java objects.


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.