2020-04-18

Hibernate JPA @PersistenceContext Example

@PersistenceContext

The @PersistenceContext annotation is used to specify the EntityManager that needs to be injected as a dependency.

PersistenceContext Expresses a dependency on a container-managed EntityManager and its associated persistence context.

Inject the default EntityManager

@PersistenceContext
private EntityManager em;
To inject a specific Persistence Context, you can use the @PersistenceContext annotation, and you can even pass EntityManager-specific properties using the @PersistenceProperty annotation.

Inject a configurable EntityManager

@PersistenceContext(
    unitName = "CRM",
    properties = {
        @PersistenceProperty(
            name="org.hibernate.flushMode",
            value= "MANUAL"
        )
    }
)
private EntityManager entityManager;


No comments:

Post a Comment