Stijn Geukens
Stijn Geukens

Reputation: 15628

Hibernate: get persisted state of dirty entity

Taking following code:

MyEntity e = dao.getEntity(1);
e.setProp1(someVal);
e.setProp2(otherVal);

MyEntity eOld = dao.getEntity(1);

If I do it like this then e will get updated (because Hibernate detected it is dirty) and eOld will have the same property values (prop1, prop2) a e. Is there a way to get the persisted state of this dirty entity (as it is in the database)?

Upvotes: 0

Views: 438

Answers (2)

Bitmap
Bitmap

Reputation: 12538

Try:

<property name="defaultAutoCommit" value="false" />

Or alternative use detach and re-attach when ready to persist.

dao.detach(e);
...
e.setProp1("AnotherVal"); //not propatated to the database
dao.merge(cat);  // update 

Upvotes: 2

Stijn Geukens
Stijn Geukens

Reputation: 15628

Actually I may already have found the solution myself...

I had already tried evicting eOld but that doesn't make since, I need to evict e before retrieving eOld and after the compare (for auditing) reattach (merge) e to the session again. It seems to work in any case.

Upvotes: 0

Related Questions