Annotation Interface PersistenceContext


@Repeatable(PersistenceContexts.class) @Target({TYPE,METHOD,FIELD}) @Retention(RUNTIME) public @interface PersistenceContext
Expresses a dependency on a container-managed EntityManager and its associated persistence context. When this annotation occurs on a method or field of a managed bean, it declares an injection point of type EntityManager.
@PersistenceContext EntityManager manager;
When the annotation occurs on a managed bean class, it assigns a name to the EntityManager in the environment referencing context java:comp/env of the containing module.
@PersistenceContext(name = "LibraryManager")
class Bean
    ...
    EntityManagerFactory =
            new InitialContext()
                    .lookup("java:comp/env/LibraryManager");
    ...
Since:
1.0
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    (Optional) The name at which the EntityManager is accessed in the environment referencing context.
    (Optional) Properties for the container or persistence provider.
    (Optional) Specifies whether the persistence context is always automatically synchronized with the current transaction or whether the persistence context must be explicitly joined to the current transaction by means of the EntityManager.joinTransaction() method.
    (Optional) Specifies whether a transaction-scoped or extended persistence context is required.
    (Optional) The name of the persistence unit as defined in the persistence.xml file.
  • Element Details

    • name

      String name
      (Optional) The name at which the EntityManager is accessed in the environment referencing context. If the specified name does not begin with java:, then the prefix java:comp/env is assumed. This member is not usually specified when @PersistenceUnit annotates an injection point.
      Default:
      ""
    • unitName

      String unitName
      (Optional) The name of the persistence unit as defined in the persistence.xml file. This member is optional if there is only one persistence unit defined by the containing module.
      Default:
      ""
    • type

      (Optional) Specifies whether a transaction-scoped or extended persistence context is required.
      Default:
      TRANSACTION
    • synchronization

      SynchronizationType synchronization
      (Optional) Specifies whether the persistence context is always automatically synchronized with the current transaction or whether the persistence context must be explicitly joined to the current transaction by means of the EntityManager.joinTransaction() method.
      Since:
      2.1
      Default:
      SYNCHRONIZED
    • properties

      PersistenceProperty[] properties
      (Optional) Properties for the container or persistence provider. Vendor-specific properties may be included in this set of properties. Properties that are not recognized by a vendor are ignored.
      Default:
      {}