I know there are a lot of discussions about this famous detached entity passed to persist error. I met that error. I well linked my entities, but that wasn't the problem: I was linking an entity to an already existing one. I tried merge()
instead of persist()
, which well executed the transaction. I enquired for that merge()
method, and I learnt here and there the main point: it copies the state of the passed object. Right.
My question is, instead of
em.persist(e); e.anything();
is it a bad idea to use
e = em.merge(e); e.anything();
?
If I well understood, merge()
returns the managed instance that the state was merged to, which should mean that overwriting the given entity with the returned one gives access to the persistent entity, doesn't it?
I saw that "solution" nowhere, so I'm asking. Mea culpa if it's a duplicated question :)