- java.lang.Object
-
- jakarta.persistence.PersistenceConfiguration
-
public class PersistenceConfiguration extends java.lang.Object
Represents a configuration of a persistence unit, allowing programmatic creation of anEntityManagerFactory
. The configuration options available via this API reflect the similarly-named elements of thepersistence.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 CDIProducer
may be used to make theEntityManagerFactory
available as a CDI managed bean. {@snippet :- Since:
- 3.2
- See Also:
Persistence.createEntityManagerFactory(PersistenceConfiguration)
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
CACHE_MODE
String specifying aSharedCacheMode
.static java.lang.String
JDBC_DATASOURCE
An instance ofjavax.sql.DataSource
.static java.lang.String
JDBC_DRIVER
Fully qualified name of the JDBC driver class.static java.lang.String
JDBC_PASSWORD
Password for JDBC authentication.static java.lang.String
JDBC_URL
JDBC URL.static java.lang.String
JDBC_USER
Username for JDBC authentication.private java.lang.String
jtaDataSource
static java.lang.String
LOCK_TIMEOUT
Default pessimistic lock timeout hint.private java.util.List<java.lang.Class<?>>
managedClasses
private java.util.List<java.lang.String>
mappingFileNames
private java.lang.String
name
private java.lang.String
nonJtaDataSource
private java.util.Map<java.lang.String,java.lang.Object>
properties
private java.lang.String
provider
static java.lang.String
QUERY_TIMEOUT
Default query timeout hint.static java.lang.String
SCHEMAGEN_CREATE_SCRIPT_SOURCE
An application-provided SQL script to be executed when the schema is created.static java.lang.String
SCHEMAGEN_CREATE_SOURCE
The source of artifacts to be created.static java.lang.String
SCHEMAGEN_CREATE_TARGET
The provider-generated SQL script which creates the schema when "jakarta.persistence.schema-generation.scripts.action" is set.static java.lang.String
SCHEMAGEN_DATABASE_ACTION
The action to be performed against the database.static java.lang.String
SCHEMAGEN_DROP_SCRIPT_SOURCE
An application-provided SQL script to be executed when the schema is dropped.static java.lang.String
SCHEMAGEN_DROP_SOURCE
The source of artifacts to be dropped.static java.lang.String
SCHEMAGEN_DROP_TARGET
The provider-generated SQL script which drops the schema when "jakarta.persistence.schema-generation.scripts.action" is set.static java.lang.String
SCHEMAGEN_SCRIPTS_ACTION
The action to be generated as a SQL script.private SharedCacheMode
sharedCacheMode
private PersistenceUnitTransactionType
transactionType
static java.lang.String
VALIDATION_FACTORY
An instance ofjakarta.validation.ValidatorFactory
,static java.lang.String
VALIDATION_GROUP_PRE_PERSIST
Target groups for validation atPrePersist
.static java.lang.String
VALIDATION_GROUP_PRE_REMOVE
Target groups for validation atPreRemove
.static java.lang.String
VALIDATION_GROUP_PRE_UPDATE
Target groups for validation atPreUpdate
.private ValidationMode
validationMode
-
Constructor Summary
Constructors Constructor Description PersistenceConfiguration(java.lang.String name)
Create a new empty configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EntityManagerFactory
createEntityManagerFactory()
Create a newEntityManagerFactory
based on this configuration.java.lang.String
jtaDataSource()
The JNDI name of a JTAjavax.sql.DataSource
.PersistenceConfiguration
jtaDataSource(java.lang.String dataSourceJndiName)
Specify the JNDI name of a JTAjavax.sql.DataSource
.PersistenceConfiguration
managedClass(java.lang.Class<?> managedClass)
java.util.List<java.lang.Class<?>>
managedClasses()
The configured managed classes, that is, a list of classes annotatedEntity
,Embeddable
,MappedSuperclass
, orConverter
.PersistenceConfiguration
mappingFile(java.lang.String name)
Add the path of an XML mapping file loaded as a resource to the configuration.java.util.List<java.lang.String>
mappingFiles()
The configured resource paths of XML mapping files.java.lang.String
name()
The name of the persistence unit, which may be used by the persistence provider for logging and error reporting.java.lang.String
nonJtaDataSource()
The JNDI name of a non-JTAjavax.sql.DataSource
.PersistenceConfiguration
nonJtaDataSource(java.lang.String dataSourceJndiName)
Specify the JNDI name of a non-JTAjavax.sql.DataSource
.java.util.Map<java.lang.String,java.lang.Object>
properties()
Standard and vendor-specific property settings.PersistenceConfiguration
properties(java.util.Map<java.lang.String,?> properties)
Set multiple properties of this persistence unit.PersistenceConfiguration
property(java.lang.String name, java.lang.Object value)
Set a property of this persistence unit.java.lang.String
provider()
The fully-qualified name of a concrete class implementingPersistenceProvider
.PersistenceConfiguration
provider(java.lang.String providerClassName)
Specify the persistence provider.SharedCacheMode
sharedCacheMode()
The shared cache mode.PersistenceConfiguration
sharedCacheMode(SharedCacheMode sharedCacheMode)
Specify the shared cache mode for the persistence unit.PersistenceUnitTransactionType
transactionType()
The transaction type.PersistenceConfiguration
transactionType(PersistenceUnitTransactionType transactionType)
Specify the transaction type for the persistence unit.ValidationMode
validationMode()
The validation mode,ValidationMode.AUTO
by default.PersistenceConfiguration
validationMode(ValidationMode validationMode)
Specify the validation mode for the persistence unit.
-
-
-
Field Detail
-
JDBC_DRIVER
public static final java.lang.String JDBC_DRIVER
Fully qualified name of the JDBC driver class.- See Also:
- Constant Field Values
-
JDBC_URL
public static final java.lang.String JDBC_URL
JDBC URL.- See Also:
- Constant Field Values
-
JDBC_USER
public static final java.lang.String JDBC_USER
Username for JDBC authentication.- See Also:
- Constant Field Values
-
JDBC_PASSWORD
public static final java.lang.String JDBC_PASSWORD
Password for JDBC authentication.- See Also:
- Constant Field Values
-
JDBC_DATASOURCE
public static final java.lang.String JDBC_DATASOURCE
An instance ofjavax.sql.DataSource
.- See Also:
- Constant Field Values
-
LOCK_TIMEOUT
public static final java.lang.String LOCK_TIMEOUT
Default pessimistic lock timeout hint.- See Also:
- Constant Field Values
-
QUERY_TIMEOUT
public static final java.lang.String QUERY_TIMEOUT
Default query timeout hint.- See Also:
- Constant Field Values
-
SCHEMAGEN_DATABASE_ACTION
public static final java.lang.String SCHEMAGEN_DATABASE_ACTION
The action to be performed against the database.Standard actions are:
none
,create
,drop
,drop-and-create
,validate
.- See Also:
- Constant Field Values
-
SCHEMAGEN_SCRIPTS_ACTION
public static final java.lang.String 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:
- Constant Field Values
-
SCHEMAGEN_CREATE_SOURCE
public static final java.lang.String 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:
- Constant Field Values
-
SCHEMAGEN_DROP_SOURCE
public static final java.lang.String 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:
- Constant Field Values
-
SCHEMAGEN_CREATE_SCRIPT_SOURCE
public static final java.lang.String SCHEMAGEN_CREATE_SCRIPT_SOURCE
An application-provided SQL script to be executed when the schema is created.- See Also:
- Constant Field Values
-
SCHEMAGEN_DROP_SCRIPT_SOURCE
public static final java.lang.String SCHEMAGEN_DROP_SCRIPT_SOURCE
An application-provided SQL script to be executed when the schema is dropped.- See Also:
- Constant Field Values
-
SCHEMAGEN_CREATE_TARGET
public static final java.lang.String SCHEMAGEN_CREATE_TARGET
The provider-generated SQL script which creates the schema when "jakarta.persistence.schema-generation.scripts.action" is set.- See Also:
- Constant Field Values
-
SCHEMAGEN_DROP_TARGET
public static final java.lang.String SCHEMAGEN_DROP_TARGET
The provider-generated SQL script which drops the schema when "jakarta.persistence.schema-generation.scripts.action" is set.- See Also:
- Constant Field Values
-
VALIDATION_FACTORY
public static final java.lang.String VALIDATION_FACTORY
An instance ofjakarta.validation.ValidatorFactory
,- See Also:
- Constant Field Values
-
VALIDATION_GROUP_PRE_PERSIST
public static final java.lang.String VALIDATION_GROUP_PRE_PERSIST
Target groups for validation atPrePersist
.- See Also:
- Constant Field Values
-
VALIDATION_GROUP_PRE_UPDATE
public static final java.lang.String VALIDATION_GROUP_PRE_UPDATE
Target groups for validation atPreUpdate
.- See Also:
- Constant Field Values
-
VALIDATION_GROUP_PRE_REMOVE
public static final java.lang.String VALIDATION_GROUP_PRE_REMOVE
Target groups for validation atPreRemove
.- See Also:
- Constant Field Values
-
CACHE_MODE
public static final java.lang.String CACHE_MODE
String specifying aSharedCacheMode
.Defined for use with
Persistence.createEntityManagerFactory(String, Map)
. Clients of thisPersistenceConfiguration
class should usesharedCacheMode(SharedCacheMode)
.- See Also:
- Constant Field Values
-
name
private final java.lang.String name
-
provider
private java.lang.String provider
-
jtaDataSource
private java.lang.String jtaDataSource
-
nonJtaDataSource
private java.lang.String nonJtaDataSource
-
sharedCacheMode
private SharedCacheMode sharedCacheMode
-
validationMode
private ValidationMode validationMode
-
transactionType
private PersistenceUnitTransactionType transactionType
-
managedClasses
private final java.util.List<java.lang.Class<?>> managedClasses
-
mappingFileNames
private final java.util.List<java.lang.String> mappingFileNames
-
properties
private final java.util.Map<java.lang.String,java.lang.Object> properties
-
-
Constructor Detail
-
PersistenceConfiguration
public PersistenceConfiguration(java.lang.String name)
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 Detail
-
createEntityManagerFactory
public EntityManagerFactory createEntityManagerFactory()
Create a newEntityManagerFactory
based on this configuration.- Throws:
PersistenceException
- if required configuration is missing or if the factory could not be created
-
name
public java.lang.String 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
public PersistenceConfiguration provider(java.lang.String providerClassName)
Specify the persistence provider.- Parameters:
providerClassName
- the qualified name of the persistence provider class- Returns:
- this configuration
-
provider
public java.lang.String provider()
The fully-qualified name of a concrete class implementingPersistenceProvider
.- Returns:
- the qualified name of the persistence provider class.
-
jtaDataSource
public PersistenceConfiguration jtaDataSource(java.lang.String dataSourceJndiName)
Specify the JNDI name of a JTAjavax.sql.DataSource
.- Parameters:
dataSourceJndiName
- the JNDI name of a JTA datasource- Returns:
- this configuration
-
jtaDataSource
public java.lang.String jtaDataSource()
The JNDI name of a JTAjavax.sql.DataSource
.- Returns:
- the configured JTA datasource, if any, or null
-
nonJtaDataSource
public PersistenceConfiguration nonJtaDataSource(java.lang.String dataSourceJndiName)
Specify the JNDI name of a non-JTAjavax.sql.DataSource
.- Parameters:
dataSourceJndiName
- the JNDI name of a non-JTA datasource- Returns:
- this configuration
-
nonJtaDataSource
public java.lang.String nonJtaDataSource()
The JNDI name of a non-JTAjavax.sql.DataSource
.- Returns:
- the configured non-JTA datasource, if any, or null
-
managedClass
public PersistenceConfiguration managedClass(java.lang.Class<?> managedClass)
- Parameters:
managedClass
- the managed class- Returns:
- this configuration
-
managedClasses
public java.util.List<java.lang.Class<?>> managedClasses()
The configured managed classes, that is, a list of classes annotatedEntity
,Embeddable
,MappedSuperclass
, orConverter
.- Returns:
- all configured managed classes
-
mappingFile
public PersistenceConfiguration mappingFile(java.lang.String name)
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
public java.util.List<java.lang.String> mappingFiles()
The configured resource paths of XML mapping files.- Returns:
- all configured mapping file resource paths
-
transactionType
public PersistenceConfiguration transactionType(PersistenceUnitTransactionType transactionType)
Specify the transaction type for the persistence unit.- Parameters:
transactionType
- the transaction type- Returns:
- this configuration
-
transactionType
public PersistenceUnitTransactionType 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
-
sharedCacheMode
public PersistenceConfiguration sharedCacheMode(SharedCacheMode sharedCacheMode)
Specify the shared cache mode for the persistence unit.- Parameters:
sharedCacheMode
- the shared cache mode- Returns:
- this configuration
-
sharedCacheMode
public SharedCacheMode sharedCacheMode()
The shared cache mode. The default behavior is unspecified and provider-specific.- Returns:
- the shared cache mode
-
validationMode
public PersistenceConfiguration validationMode(ValidationMode validationMode)
Specify the validation mode for the persistence unit.- Parameters:
validationMode
- the shared cache mode- Returns:
- this configuration
-
validationMode
public ValidationMode validationMode()
The validation mode,ValidationMode.AUTO
by default.- Returns:
- the validation mode
-
property
public PersistenceConfiguration property(java.lang.String name, java.lang.Object value)
Set a property of this persistence unit.- Parameters:
name
- the property namevalue
- the property value- Returns:
- this configuration
-
properties
public PersistenceConfiguration properties(java.util.Map<java.lang.String,?> properties)
Set multiple properties of this persistence unit.- Parameters:
properties
- the properties- Returns:
- this configuration
-
properties
public java.util.Map<java.lang.String,java.lang.Object> properties()
Standard and vendor-specific property settings.- Returns:
- the configured properties
-
-