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.