Sunday, 25 August 2013

Java - JPA - (Un)instantiated List causes changes to one of its objects to be reverted

Java - JPA - (Un)instantiated List causes changes to one of its objects to
be reverted

Why does opening an uninstantiated List of (entity) objects revert changes
already made to any one of said objects within the same transaction?
Scenario:
A, B and C are all Entities
A holds a reference to a particular instance of B, retrievable with a.getB();
C has a list of B objects, retrievable with c.getListOfB()
The following is true: c.getListOfB().contains(a.getB())
If I do the following within a transaction:
B b = a.getB();
b.setOk(true); // <-- Changed from false to true
and then follow with:
c.getListOfB().isEmpty() // <-- Any function will do, I just used
.isEmpty() to test it
Then b IMMEDIATELY has OK value set to false again :/
This happens within the same Transaction, before committing to DB.
Can someone please be so kind as to explain to me why EntityManager isn't
aware of the changes already made to the above entity within the
Transaction, and how to I can make sure it keeps them? This led me on a
long bug hunt and I'm literally at loss of words on how to find any
helpful results online.

No comments:

Post a Comment