Class Persistence
java.lang.Object
jakarta.persistence.Persistence
Bootstrap class used to obtain an
Since
EntityManagerFactory in
Java SE environments.
EntityManagerFactory factory =
Persistence.createEntityManagerFactory("Library", // name of the persistence unit
// property overrides
Map.of(Persistence.ConnectionProperties.JDBC_USER, userName,
Persistence.ConnectionProperties.JDBC_PASSWORD, password));
createEntityManagerFactory(String) is an expensive operation,
and since the EntityManagerFactory is a heavyweight object,
the application program should create no more than one factory for
each persistence unit.
This class also allows schema management to be decoupled from creation of the entity manager factory.
Persistence.generateSchema("Library", // name of the persistence unit
// property overrides
Map.of(Persistence.SchemaManagementProperties.SCHEMAGEN_DATABASE_ACTION, "create",
Persistence.ConnectionProperties.JDBC_USER, userName,
Persistence.ConnectionProperties.JDBC_PASSWORD, password));
The Persistence class may be used to obtain a
PersistenceUtil instance in both Jakarta EE
and Java SE environments.
boolean loaded = Persistence.getPersistenceUtil().isLoaded(entity);
The nested interfaces of this class list the standard configuration properties defined by the Jakarta Persistence specification.
- Since:
- 1.0
- See Also:
- API note:
- The
Persistenceclass is available in a Jakarta EE container environment. However, support for the Java SE bootstrapping APIs is not required in container environments.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceProperties used to control the integration with CDI.static interfaceProperties used to configure the second-level cache.static interfaceProperties used to connect to the database via JDBC in Java SE.static interfaceProperties used to specify the database platform when JDBC metadata is not available.static interfaceProperties used to optimize the interaction with JDBC.static interfaceProperties used to control the schema management tooling.static interfaceProperties used to override elements the configuration of a persistence unit inpersistence.xml.static interfaceProperties used to control the integration with Bean Validation. -
Method Summary
Modifier and TypeMethodDescriptionstatic EntityManagerFactorycreateEntityManagerFactory(PersistenceConfiguration configuration) Create and return anEntityManagerFactoryfor the named persistence unit, using the given properties.static EntityManagerFactorycreateEntityManagerFactory(String unitName) Create and return anEntityManagerFactoryfor the named persistence unit.static EntityManagerFactorycreateEntityManagerFactory(String unitName, Map<?, ?> properties) Create and return anEntityManagerFactoryfor the named persistence unit, using the given properties.static voidgenerateSchema(PersistenceConfiguration configuration) Create database schemas and/or tables and/or create DDL scripts as determined by the supplied properties.static voidgenerateSchema(String unitName, Map<?, ?> map) Create database schemas and/or tables and/or create DDL scripts as determined by the supplied properties.static PersistenceUtilReturn thePersistenceUtilinstance
-
Method Details
-
createEntityManagerFactory
Create and return anEntityManagerFactoryfor the named persistence unit.- Parameters:
unitName- the name of the persistence unit; usually the name specified by thenameattribute of the<persistence-unit>element in thepersistence.xmlfile- Returns:
- an
EntityManagerFactoryfor the named persistence unit - API note:
- This operation is very expensive. It should usually be called just once for each persistence unit.
-
createEntityManagerFactory
@Nonnull public static EntityManagerFactory createEntityManagerFactory(@Nonnull String unitName, @Nullable Map<?, ?> properties) Create and return anEntityManagerFactoryfor the named persistence unit, using the given properties. Standard properties are enumerated byPersistence.UnitProperties,Persistence.CacheProperties,Persistence.ConnectionProperties,Persistence.JdbcProperties,Persistence.DatabaseProperties,Persistence.SchemaManagementProperties,Persistence.ValidationProperties, andPersistence.BeanManagementProperties. A provider might recognize additional vendor-specific properties. Property values specified via the second argument of this method override property values specified in thepersistence.xmlfile.- Parameters:
unitName- the name of the persistence unit; usually the name specified by thenameattribute of the<persistence-unit>element in thepersistence.xmlfileproperties- additional properties to use when creating the factory. These properties may include properties to control schema generation. The values of these properties override any values that may have been configured elsewhere.- Returns:
- an
EntityManagerFactoryfor the named persistence unit - API note:
- This operation is very expensive. It should usually be called just once for each persistence unit.
-
createEntityManagerFactory
@Nonnull public static EntityManagerFactory createEntityManagerFactory(@Nonnull PersistenceConfiguration configuration) Create and return anEntityManagerFactoryfor the named persistence unit, using the given properties.- Parameters:
configuration- configuration of the persistence unit- Returns:
- the factory that creates
EntityManagers configured according to the specified persistence unit - Since:
- 3.2
- API note:
- This operation is very expensive. It should usually be called just once for each persistence unit.
-
generateSchema
Create database schemas and/or tables and/or create DDL scripts as determined by the supplied properties. Standard properties are enumerated byPersistence.SchemaManagementProperties,Persistence.DatabaseProperties,Persistence.ConnectionProperties, andPersistence.UnitProperties. A provider might recognize additional vendor-specific properties. Property values specified via the second argument of this method override property values specified in thepersistence.xmlfile.Called when schema generation is to occur as a separate phase from creation of the entity manager factory.
- Parameters:
unitName- the name of the persistence unit; usually the name specified by thenameattribute of the<persistence-unit>element in thepersistence.xmlfilemap- properties for schema generation; these may also contain provider-specific properties. The values of these properties override any values that may have been configured elsewhere.- Throws:
PersistenceException- if insufficient or inconsistent configuration information is provided or if schema generation otherwise fails.- Since:
- 2.1
-
generateSchema
Create database schemas and/or tables and/or create DDL scripts as determined by the supplied properties.Called when schema generation is to occur as a separate phase from creation of the entity manager factory.
- Parameters:
configuration- configuration of the persistence unit- Throws:
PersistenceException- if insufficient or inconsistent configuration information is provided or if schema generation otherwise fails.- Since:
- 4.0
-
getPersistenceUtil
Return thePersistenceUtilinstance- Returns:
PersistenceUtilinstance- Since:
- 2.0
-