JPA Method
in jakarta.persistence.EntityManager

T merge(
  T entity
)


Merge the state of the given new or detached entity instance into the current persistence context, resulting in, respectively, an insert or possible update when the persistence context is synchronized with the database. Return a managed instance with the same persistent state as the given entity instance, but a distinct Java object identity. If the given entity is detached, the returned entity has the same persistent identity. This operation cascades to every entity related by an association marked cascade=MERGE. If the given entity instance is managed, that is, if it belongs to this persistence context, and has not been marked for removal, it is itself ignored, but the operation still cascades, and it is returned directly.
Parameters:
entity - a new, managed, or detached entity instance
Return:
the managed instance that the state was merged to
Throws:
IllegalArgumentException - if the instance is not an entity or is a removed entity
TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager of that is of type {@link PersistenceContextType#TRANSACTION}
Since:
JPA 1.0