Jakarta Persistence / Spring Data JPA
EntityManager.merge
Copy entity state into a managed instance and return it. Boot 4.0.10 companion project; source-reviewed, not executed.
Formal Syntax & Structure
java syntaxjava
em.merge(first)Working Example
Example Snippetjava
package example.catalog;import jakarta.persistence.EntityManager;public final class ContextWalk { private ContextWalk() {} // Caller owns an active transaction and the EntityManager. public static void demonstrate(EntityManager em, Long id) { Product first = em.find(Product.class, id); Product second = em.find(Product.class, id); if (first == null || first != second) throw new IllegalStateException("Expected managed identity"); em.detach(first); if (em.contains(first)) throw new IllegalStateException("Expected detached entity"); Product managed = em.merge(first); if (!em.contains(managed) || managed == first) throw new IllegalStateException("Expected managed copy"); em.flush(); // synchronization, not commit }}