I understand that JPA does not work as I expected, since often I get multiple instances of the same object in a session.
Here's an example study: a parent has a set of children mapped to @OneToMany
In one method:
- get an instance of the parent P object
- Download the collection of the child using P.getChilds (): it contains an instance of C1 and C2
- then find a specific Child with an optimized JPA request for which Parent P has a parameter: Child C = dao.getSpecificChild (P)
Here I would expect C to become one of two already loaded instances (from C1 or C2). I thought JPA would check for an existing instance in the current session. But JPA will load a new instance of C (regardless of whether it has C1 or C2 here).
So, I get two different instances of C.
My question is: is this the expected behavior? If so, how can I reconcile entity instances in a session?
amuses
source
share