1

I'm trying to reattach to the session an object from the HTTP session that was originally retrieved from the DB, I do this by calling session.lock(object, LockMode.None) and even though lock does not cascade this works all right for me because it does not push updates to the DB like merge does (the lock is required to open a detail view in a pop-up and the actual saving would occur later on the main window). Now to my surprise I have found that if my entity has a one to many relation any changes on that collection would cause a HibernateException "Reassociated object has dirty collection".

How am I supposed to reattach objects to the session without updating the DB or discarding the changes on the object then?

Here is the situation as code

  EntityA t = createAnEntityA();
  Session sess = factory.openSession();
  sess.beginTransaction();
  sess.save(t);
  sess.getTransaction().commit();
  sess.close();
  // t is now saved on the DB but in dettached state



  // change a simple property
  sess = factory.openSession();
  sess.beginTransaction();
  t.setPropertyB("B");
  sess.lock(t, LockMode.NONE);
  // t is attached again, you won't get LazyInitializationException
  // by calling its properties, although you have to be careful 
  // because the reattachment does not cascade to children
  sess.getTransaction().commit();
  sess.close();
  // no updates went to the DB because setPropertyB was called
  // when t was still dettached

  // now change a collection
  EntityC c = createAnEntityC();
  t.getCollectionPropertyC().add(c);
  sess = factory.openSession();
  sess.beginTransaction();
  sess.lock(t, LockMode.NONE);
  // Exception is thrown :-(
  sess.getTransaction().commit();
  sess.close();
ilcavero
  • 3,012
  • 1
  • 31
  • 27
  • Is it possible that your solution is somewhere between the answers of this question: http://stackoverflow.com/questions/912659/what-is-the-proper-way-to-re-attach-detached-objects-in-hibernate – K.C. Mar 29 '12 at 13:37
  • well merge or update would make changes to the DB immediately and refresh would lose the changes in the object, what I want is a simple reattach so I don't get Lazy loading exceptions when I open my detail view popp-up – ilcavero Mar 29 '12 at 14:02

1 Answers1

1

I'm afraid currently it's not possible without hitting DB. Apparently, the exception is happening because of lock command.

There's a Jira reporting this behavior.
https://hibernate.atlassian.net/browse/HHH-511

It has 2 patches to fix the problem. You could try those patches.

But if you problem is only Lazy loading of collection, you could consider using Open Session In View pattern. (maybe not the best pattern, but it can work for your case)

Hendrik
  • 5,085
  • 24
  • 56
RicardoS
  • 2,088
  • 1
  • 21
  • 22
  • The bug you link is not really related to my need, but after much research I have to agree with you that there is no possible way, yet another reason to dislike Hibernate. My work around so far is catching the LazyInitialiationException and calling lock on that catch, that way I only lock the object the first time it was not yet initialized and hence still unmodified – ilcavero Mar 31 '12 at 22:48