users@glassfish.java.net

Re: how to persist a new entity that refers to already existing entity

From: Wolfram Rittmeyer <w.rittmeyer_at_jsptutorial.org>
Date: Sun, 01 Mar 2009 15:56:39 +0100

gufiya wrote:
> Thanks for the answers.
> Indeed, merge() sounds better than explicit find() operation.
> But still, doesn't it mean an additional access to the database?
> One underlying query for merge() and another for persist()?

Not necessarily. The EntityManager first looks if it already contains a
managed object of the same type with the same identity. If so the state
of your detached object is copied to the managed object and persisted at
the end of the transaction (if no rollback occurs).

--
Wolfram Rittmeyer