EntityManagerFactory
. The configuration options
available via this API reflect the similarly-named elements of the
persistence.xml
file.
This API may not be used to configure a container-managed persistence unit. That is, the configured persistence unit should be considered a Java SE persistence unit, even when this API is used within the Jakarta EE environment.
If injection of the EntityManagerFactory
is required, a CDI
Producer
may be used to make the EntityManagerFactory
available as a CDI managed bean.
@Produces @ApplicationScoped @Documents
EntityManagerFactory configure() {
return new PersistenceConfiguration()
.name("DocumentData")
.nonJtaDataSource("java:global/jdbc/DocumentDatabase")
.managedClass(Document.class)
.createEntityManagerFactory();
}
Similarly, if injection of an EntityManager
is required,
a CDI Producer
method/Disposer
method pair may be
used to make the EntityManager
available as a CDI managed
bean.
@Produces @TransactionScoped @Documents
EntityManager create(@Documents EntityManagerFactory factory) {
return factory.createEntityManager();
}
void close(@Disposes @Documents EntityManager entityManager) {
entityManager.close();
}
It is intended that persistence providers define subclasses of this class with vendor-specific configuration options. A provider must support configuration via any instance of this class or of any subclass of this class.
- Since:
- 3.2
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
String specifying aSharedCacheMode
.static final String
An instance ofjavax.sql.DataSource
.static final String
Fully qualified name of the JDBC driver class.static final String
Password for JDBC authentication.static final String
JDBC URL.static final String
Username for JDBC authentication.static final String
Default pessimistic lock timeout hint.static final String
Default query timeout hint.static final String
An application-provided SQL script to be executed when the schema is created.static final String
The source of artifacts to be created.static final String
The provider-generated SQL script which creates the schema when "jakarta.persistence.schema-generation.scripts.action" is set.static final String
The action to be performed against the database.static final String
An application-provided SQL script to be executed when the schema is dropped.static final String
The source of artifacts to be dropped.static final String
The provider-generated SQL script which drops the schema when "jakarta.persistence.schema-generation.scripts.action" is set.static final String
The action to be generated as a SQL script.static final String
An instance ofjakarta.validation.ValidatorFactory
,static final String
Target groups for validation atPrePersist
.static final String
Target groups for validation atPreRemove
.static final String
Target groups for validation atPreUpdate
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionCreate a newEntityManagerFactory
based on this configuration.The JNDI name of a JTAjavax.sql.DataSource
.jtaDataSource
(String dataSourceJndiName) Specify the JNDI name of a JTAjavax.sql.DataSource
.managedClass
(Class<?> managedClass) The configured managed classes, that is, a list of classes annotatedEntity
,Embeddable
,MappedSuperclass
, orConverter
.mappingFile
(String name) Add the path of an XML mapping file loaded as a resource to the configuration.The configured resource paths of XML mapping files.name()
The name of the persistence unit, which may be used by the persistence provider for logging and error reporting.The JNDI name of a non-JTAjavax.sql.DataSource
.nonJtaDataSource
(String dataSourceJndiName) Specify the JNDI name of a non-JTAjavax.sql.DataSource
.Standard and vendor-specific property settings.properties
(Map<String, ?> properties) Set multiple properties of this persistence unit.Set a property of this persistence unit.provider()
The fully-qualified name of a concrete class implementingPersistenceProvider
.Specify the persistence provider.The shared cache mode.sharedCacheMode
(SharedCacheMode sharedCacheMode) Specify the shared cache mode for the persistence unit.The transaction type.transactionType
(PersistenceUnitTransactionType transactionType) Specify the transaction type for the persistence unit.The validation mode,ValidationMode.AUTO
by default.validationMode
(ValidationMode validationMode) Specify the validation mode for the persistence unit.
-
Field Details
-
JDBC_DRIVER
Fully qualified name of the JDBC driver class.- See Also:
-
JDBC_URL
JDBC URL.- See Also:
-
JDBC_USER
Username for JDBC authentication.- See Also:
-
JDBC_PASSWORD
Password for JDBC authentication.- See Also:
-
JDBC_DATASOURCE
An instance ofjavax.sql.DataSource
.- See Also:
-
LOCK_TIMEOUT
Default pessimistic lock timeout hint.- See Also:
-
QUERY_TIMEOUT
Default query timeout hint.- See Also:
-
SCHEMAGEN_DATABASE_ACTION
The action to be performed against the database.Standard actions are:
none
,create
,drop
,drop-and-create
,validate
.- See Also:
-
SCHEMAGEN_SCRIPTS_ACTION
The action to be generated as a SQL script.The script is generated in the location specified by "jakarta.persistence.schema-generation.create-target" or "jakarta.persistence.schema-generation.drop-target".
Standard actions are:
none
,create
,drop
,drop-and-create
.- See Also:
-
SCHEMAGEN_CREATE_SOURCE
The source of artifacts to be created.Standard sources are:
metadata
,script
,metadata-then-script
,script-then-metadata
.The location of the script source is specified by "jakarta.persistence.schema-generation.create-script-source".
- See Also:
-
SCHEMAGEN_DROP_SOURCE
The source of artifacts to be dropped.Standard sources are:
metadata
,script
,metadata-then-script
,script-then-metadata
.The location of the script source is specified by "jakarta.persistence.schema-generation.drop-script-source".
- See Also:
-
SCHEMAGEN_CREATE_SCRIPT_SOURCE
An application-provided SQL script to be executed when the schema is created.- See Also:
-
SCHEMAGEN_DROP_SCRIPT_SOURCE
An application-provided SQL script to be executed when the schema is dropped.- See Also:
-
SCHEMAGEN_CREATE_TARGET
The provider-generated SQL script which creates the schema when "jakarta.persistence.schema-generation.scripts.action" is set.- See Also:
-
SCHEMAGEN_DROP_TARGET
The provider-generated SQL script which drops the schema when "jakarta.persistence.schema-generation.scripts.action" is set.- See Also:
-
VALIDATION_FACTORY
An instance ofjakarta.validation.ValidatorFactory
,- See Also:
-
VALIDATION_GROUP_PRE_PERSIST
Target groups for validation atPrePersist
.- See Also:
-
VALIDATION_GROUP_PRE_UPDATE
Target groups for validation atPreUpdate
.- See Also:
-
VALIDATION_GROUP_PRE_REMOVE
Target groups for validation atPreRemove
.- See Also:
-
CACHE_MODE
String specifying aSharedCacheMode
.Defined for use with
Persistence.createEntityManagerFactory(String, Map)
. Clients of thisPersistenceConfiguration
class should usesharedCacheMode(SharedCacheMode)
.- See Also:
-
-
Constructor Details
-
PersistenceConfiguration
Create a new empty configuration. An empty configuration does not typically hold enough information for successful invocation ofcreateEntityManagerFactory()
.- Parameters:
name
- the name of the persistence unit, which may be used by the persistence provider for logging and error reporting
-
-
Method Details
-
createEntityManagerFactory
Create a newEntityManagerFactory
based on this configuration.- Throws:
PersistenceException
- if required configuration is missing or if the factory could not be created
-
name
The name of the persistence unit, which may be used by the persistence provider for logging and error reporting.- Returns:
- the name of the persistence unit.
-
provider
Specify the persistence provider.- Parameters:
providerClassName
- the qualified name of the persistence provider class- Returns:
- this configuration
-
provider
The fully-qualified name of a concrete class implementingPersistenceProvider
.- Returns:
- the qualified name of the persistence provider class.
-
jtaDataSource
Specify the JNDI name of a JTAjavax.sql.DataSource
.- Parameters:
dataSourceJndiName
- the JNDI name of a JTA datasource- Returns:
- this configuration
-
jtaDataSource
The JNDI name of a JTAjavax.sql.DataSource
.- Returns:
- the configured JTA datasource, if any, or null
-
nonJtaDataSource
Specify the JNDI name of a non-JTAjavax.sql.DataSource
.- Parameters:
dataSourceJndiName
- the JNDI name of a non-JTA datasource- Returns:
- this configuration
-
nonJtaDataSource
The JNDI name of a non-JTAjavax.sql.DataSource
.- Returns:
- the configured non-JTA datasource, if any, or null
-
managedClass
- Parameters:
managedClass
- the managed class- Returns:
- this configuration
-
managedClasses
The configured managed classes, that is, a list of classes annotatedEntity
,Embeddable
,MappedSuperclass
, orConverter
.- Returns:
- all configured managed classes
-
mappingFile
Add the path of an XML mapping file loaded as a resource to the configuration.- Parameters:
name
- the resource path of the mapping file- Returns:
- this configuration
-
mappingFiles
The configured resource paths of XML mapping files.- Returns:
- all configured mapping file resource paths
-
transactionType
Specify the transaction type for the persistence unit.- Parameters:
transactionType
- the transaction type- Returns:
- this configuration
-
transactionType
The transaction type.- If
PersistenceUnitTransactionType.JTA
, a JTA data source must be provided viajtaDataSource()
, or by the container. - If
PersistenceUnitTransactionType.RESOURCE_LOCAL
, database connection properties may be specified viaproperties()
, or a non-JTA datasource may be provided vianonJtaDataSource()
.
- Returns:
- the transaction type
- If
-
validationMode
Specify the validation mode for the persistence unit.- Parameters:
validationMode
- the shared cache mode- Returns:
- this configuration
-
validationMode
The validation mode,ValidationMode.AUTO
by default.- Returns:
- the validation mode
-
property
Set a property of this persistence unit.- Parameters:
name
- the property namevalue
- the property value- Returns:
- this configuration
-
properties
Set multiple properties of this persistence unit.- Parameters:
properties
- the properties- Returns:
- this configuration
-
properties
Standard and vendor-specific property settings.- Returns:
- the configured properties
-