Jakarta Persistence / Spring Data JPA

EntityManager.find

Find by primary key in the persistence context or return null if missing. Boot 4.0.10 companion project; source-reviewed, not executed.

Formal Syntax & Structure

java syntaxjava
em.find(Product.class, id)

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    }}
Java Reference