merge

Die Methode <T> T merge(T entity) gehört zum EntityManager Interface. Wenn der Methode ein neues oder ein Detached Entity übergeben wird, wird ein Managed Entity mit gleichen Attributwerten erzeugt oder (z.B. aus einem Cache) identifiziert und als Rückgabewert verwendet. Wenn der Methode ein gelöschtes Entity übergeben wird, dann wird eine IllegalArgumentException ausgelöst oder der Commit der Transaktion schlägt fehl. Managed Entities (siehe Managed Entity) werden von der Methode ignoriert. Die Methode führt also ein Upsert aus.

/**
* Merge the state of the given entity into the
* current persistence context.
* @param entity
* @return the instance that the state was merged to
* @throws IllegalArgumentException if instance is not an
* entity or is a removed entity
* @throws TransactionRequiredException if invoked on a
* container-managed entity manager of type
* PersistenceContextType.TRANSACTION and there is
* no transaction.
*/