Reputation: 299
I realize JPA doesn't work as I expected, as I often end up with multiple instance of a single entity in a session.
Here's the study case: A parent has a collection of child mapped with @OneToMany
In a single method:
Here I would expect C to be one of the two instances already loaded (of C1 or C2). I thought JPA would check the already existing instance in its current session. But JPA will load a new instance of C (whether it is C1 or C2 doesn't matter here).
So that I end up with two different instances of C.
My question is: is this the expected behavior? If it is, how can I reconcile my entity instances in a session?
cheers
Upvotes: 4
Views: 2681
Reputation: 15577
I too would expect the instance of C returned from the query to be one of the previously returned Cs (assuming its the same txn) since they are enlisted in the transaction already, and that is what DataNucleus certainly does (since it also implements JDO and that is part of the spec), and that's what an L1 cache is for.
Upvotes: 0
Reputation: 298898
how can I reconcile my entity instances in a session?
If you want to transfer state from entity a to entity b, you can do the following:
entityManager.merge(a);
entityManager.refresh(b);
Upvotes: 2