I want to compare the current value of an in-memory Hibernate entity with the value in the database:
HibernateSession sess = HibernateSessionFactory.getSession();
MyEntity newEntity = (MyEntity)sess.load(MyEntity.class, id);
newEntity.setProperty("new value");
MyEntity oldEntity = (MyEntity)sess.load(MyEntity.class, id);
// CODEBLOCK#1 evaluate differences between newEntity and oldEntity
sess.update(newEntity);
In CODEBLOCK#1 I get that newEntity.getProperty()="new value"
AND oldEntity.getProperty()="new value"
(while I expected oldEntity.getProperty()="old value"
, of course). In fact the two objects are exactly the same in memory.
I messed around with HibernateSessionFactory.getSession().evict(newEntity)
and attempted to set oldEntity=null
to get rid of it (I need it only for the comparison):
HibernateSession sess = HibernateSessionFactory.getSession();
MyEntity newEntity = (MyEntity)sess.load(MyEntity.class, id);
newEntity.setProperty("new value");
HibernateSessionFactory.getSession().evict(newEntity);
MyEntity oldEntity = (MyEntity)sess.load(MyEntity.class, id);
// CODEBLOCK#1 evaluate differences between newEntity and oldEntity
oldEntity = null;
sess.update(newEntity);
and now the two entities are distinct, but of course I get the dreaded org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
.
Any idea?
EDIT: I tried the double session strategy; I modified my HibernateSessionFactory
to implement a map of session and then...
Session session1 = HibernateSessionFactory.getSession(SessionKeys.DEFAULT);
Session session2 = HibernateSessionFactory.getSession(SessionKeys.ALTERNATE);
Entity newEntity = (Entity)entity;
newEntity.setNote("edited note");
Entity oldEntity = (Entity)session1.load(Entity.class, id);
System.out.println("NEW:" + newEntity.getNote());
System.out.println("OLD: " + oldEntity.getNote()); // HANGS HERE!!!
HibernateSessionFactory.closeSession(SessionKeys.ALTERNATE);
My unit test hangs while attempting to print the oldEntity note... :-(