Annotation Interface PersistenceUnit


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

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    (Optional) The name at which the EntityManagerFactory is accessed in the environment referencing context.
    (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 EntityManagerFactory 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:
      ""