Dear All,
I have a problem reattaching pojos across multiple sessions. These pojos are value objects - like Money or Long - in that their
value is more important than the reference. Two separate instances of the pojos are equal if they have the same value... I think the code speaks for itself...
Code:
pojo1 = session1.get(eg.ThisObj, objId);
session1.close();
//..a bit later... open a new session
pojo2 = session2.get(eg.ThisObj, objId);
session2.close();
assertFalse(pojo1==pojo2);
assertTrue(pojo1.equals(pojo2)); //value objects
//----------- later... --------------
//..open a third session
session3.lock(pojo1, LockMode.NONE);
session3.saveOrUpdate(pojo1); //is most up-to-date data
//use pojo 1 no problem...
//------ elsewhere in the system -------
// I want to start using pojo2
session3.lock(pojo2, LockMode.NONE);
//ERROR!! - Throws NonUniqueObjectException
I want to reattach this second pojo before I start using it so that I have the most recent data. This is also necessary for any LazyInitialised Collections held within it. Lock doesn't work because it uses the IdentityMap inside the session during the first half of the process, which doesn't find the existing equivalent pojo already there. Then the second half of the process tries to save the second pojo - only to find the other one sitting in the session and throwing an error.
I can't really use
Merge as I can't replace the pojo2 reference (it's held in other objects...) and I can't just use one session as the different steps span web-requests...
Any clues? This problem is causing me significant headaches. I just want a
refresh function that fixes references to the persistent session so that my Lazy Initialisation works, and merges any changes in data. This doesn't sound too much to ask for, does it?
How do i do that?
Regards,
Ben.[/code]