Class DefaultPersistenceUnitManager
- All Implemented Interfaces:
Aware
,InitializingBean
,ResourceLoaderAware
,LoadTimeWeaverAware
,PersistenceUnitManager
PersistenceUnitManager
interface.
Used as internal default by
LocalContainerEntityManagerFactoryBean
.
Supports standard JPA scanning for persistence.xml
files,
with configurable file locations, JDBC DataSource lookup and load-time weaving.
Builds a persistence unit based on the state of a PersistenceManagedTypes
,
typically built using a PersistenceManagedTypesScanner
.
The default XML file location is classpath*:META-INF/persistence.xml
,
scanning for all matching files in the classpath (as defined in the JPA specification).
DataSource names are by default interpreted as JNDI names, and no load time weaving
is available (which requires weaving to be turned off in the persistence provider).
- Since:
- 2.0
- Author:
- Juergen Hoeller, Stephane Nicoll
- See Also:
-
setPersistenceXmlLocations(java.lang.String...)
setDataSourceLookup(org.springframework.jdbc.datasource.lookup.DataSourceLookup)
setLoadTimeWeaver(org.springframework.instrument.classloading.LoadTimeWeaver)
LocalContainerEntityManagerFactoryBean.setPersistenceUnitManager(org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager)
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Default location of thepersistence.xml
file: "classpath*:META-INF/persistence.xml".protected final Log
static final String
Default persistence unit name.static final String
Default location for the persistence unit root URL: "classpath:", indicating the root of the classpath. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.Return the JDBC DataSourceLookup that provides DataSources for the persistence provider, resolving data source names inpersistence.xml
against Spring-managed DataSource instances.Return the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified inpersistence.xml
.Return the JTA-aware DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified inpersistence.xml
.Return the Spring LoadTimeWeaver to use for class instrumentation according to the JPA class transformer contract.protected final MutablePersistenceUnitInfo
getPersistenceUnitInfo
(String persistenceUnitName) Return the specified PersistenceUnitInfo from this manager's cache of processed persistence units, keeping it in the cache (i.e.Return the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager.protected boolean
Return whether an override of a same-named persistence unit is allowed.Obtain the default PersistenceUnitInfo from this manager.obtainPersistenceUnitInfo
(String persistenceUnitName) Obtain the specified PersistenceUnitInfo from this manager.protected void
Hook method allowing subclasses to customize each PersistenceUnitInfo.void
Prepare the PersistenceUnitInfos according to the configuration of this manager: scanning forpersistence.xml
files, parsing all matching files, configuring and post-processing them.void
setDataSourceLookup
(DataSourceLookup dataSourceLookup) Specify the JDBC DataSourceLookup that provides DataSources for the persistence provider, resolving data source names inpersistence.xml
against Spring-managed DataSource instances.void
setDataSources
(Map<String, DataSource> dataSources) Specify the JDBC DataSources that the JPA persistence provider is supposed to use for accessing the database, resolving data source names inpersistence.xml
against Spring-managed DataSources.void
setDefaultDataSource
(DataSource defaultDataSource) Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified inpersistence.xml
.void
setDefaultJtaDataSource
(DataSource defaultJtaDataSource) Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified inpersistence.xml
.void
setDefaultPersistenceUnitName
(String defaultPersistenceUnitName) Specify the name of the default persistence unit, if any.void
setDefaultPersistenceUnitRootLocation
(String defaultPersistenceUnitRootLocation) Set the default persistence unit root location, to be applied if no unit-specific persistence unit root could be determined.void
setLoadTimeWeaver
(LoadTimeWeaver loadTimeWeaver) Specify the Spring LoadTimeWeaver to use for class instrumentation according to the JPA class transformer contract.void
setManagedClassNameFilter
(ManagedClassNameFilter managedClassNameFilter) Set theManagedClassNameFilter
to apply on entity classes discovered using classpath scanning.void
setManagedTypes
(PersistenceManagedTypes managedTypes) Set thePersistenceManagedTypes
to use to build the list of managed types as an alternative to entity scanning.void
setMappingResources
(String... mappingResources) Specify one or more mapping resources (equivalent to<mapping-file>
entries inpersistence.xml
) for the default persistence unit.void
setPackagesToScan
(String... packagesToScan) Set whether to use Spring-based scanning for entity classes in the classpath instead of using JPA's standard scanning of jar files withpersistence.xml
markers in them.void
setPersistenceUnitPostProcessors
(PersistenceUnitPostProcessor... postProcessors) Set the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager.void
setPersistenceXmlLocation
(String persistenceXmlLocation) Specify the location of thepersistence.xml
files to load.void
setPersistenceXmlLocations
(String... persistenceXmlLocations) Specify multiple locations ofpersistence.xml
files to load.void
setResourceLoader
(ResourceLoader resourceLoader) Set the ResourceLoader that this object runs in.void
setSharedCacheMode
(SharedCacheMode sharedCacheMode) Specify the JPA 2.0 shared cache mode for all of this manager's persistence units, overriding any value inpersistence.xml
if set.void
setValidationMode
(ValidationMode validationMode) Specify the JPA 2.0 validation mode for all of this manager's persistence units, overriding any value inpersistence.xml
if set.
-
Field Details
-
DEFAULT_PERSISTENCE_XML_LOCATION
Default location of thepersistence.xml
file: "classpath*:META-INF/persistence.xml".- See Also:
-
ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION
Default location for the persistence unit root URL: "classpath:", indicating the root of the classpath.- See Also:
-
ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME
Default persistence unit name.- See Also:
-
logger
-
-
Constructor Details
-
DefaultPersistenceUnitManager
public DefaultPersistenceUnitManager()
-
-
Method Details
-
setPersistenceXmlLocation
Specify the location of thepersistence.xml
files to load. These can be specified as Spring resource locations and/or location patterns.Default is "classpath*:META-INF/persistence.xml".
-
setPersistenceXmlLocations
Specify multiple locations ofpersistence.xml
files to load. These can be specified as Spring resource locations and/or location patterns.Default is "classpath*:META-INF/persistence.xml".
- Parameters:
persistenceXmlLocations
- an array of Spring resource Strings identifying the location of thepersistence.xml
files to read
-
setDefaultPersistenceUnitRootLocation
Set the default persistence unit root location, to be applied if no unit-specific persistence unit root could be determined.Default is "classpath:", that is, the root of the current classpath (nearest root directory). To be overridden if unit-specific resolution does not work and the classpath root is not appropriate either.
-
setDefaultPersistenceUnitName
Specify the name of the default persistence unit, if any. Default is "default".Primarily applied to a scanned persistence unit without
persistence.xml
. Also applicable to selecting a default unit from several persistence units available. -
setManagedTypes
Set thePersistenceManagedTypes
to use to build the list of managed types as an alternative to entity scanning.- Parameters:
managedTypes
- the managed types- Since:
- 6.0
-
setPackagesToScan
Set whether to use Spring-based scanning for entity classes in the classpath instead of using JPA's standard scanning of jar files withpersistence.xml
markers in them. In case of Spring-based scanning, nopersistence.xml
is necessary; all you need to do is to specify base packages to search here.Default is none. Specify packages to search for autodetection of your entity classes in the classpath. This is analogous to Spring's component-scan feature (
ClassPathBeanDefinitionScanner
).Consider setting a
PersistenceManagedTypes
instead that allows the scanning logic to be optimized by AOT processing.Such package scanning defines a "default persistence unit" in Spring, which may live next to regularly defined units originating from
persistence.xml
. Its name is determined bysetDefaultPersistenceUnitName(java.lang.String)
: by default, it's simply "default".Note: There may be limitations in comparison to regular JPA scanning. In particular, JPA providers may pick up annotated packages for provider-specific annotations only when driven by
persistence.xml
. As of 4.1, Spring's scan can detect annotated packages as well if supported by the givenJpaVendorAdapter
(for example, for Hibernate).If no explicit
mapping resources
have been specified in addition to these packages, this manager looks for a defaultMETA-INF/orm.xml
file in the classpath, registering it as a mapping resource for the default unit if the mapping file is not co-located with apersistence.xml
file (in which case we assume it is only meant to be used with the persistence units defined there, like in standard JPA). -
setManagedClassNameFilter
Set theManagedClassNameFilter
to apply on entity classes discovered using classpath scanning.- Parameters:
managedClassNameFilter
- a predicate to filter entity classes- Since:
- 6.1.4
-
setMappingResources
Specify one or more mapping resources (equivalent to<mapping-file>
entries inpersistence.xml
) for the default persistence unit. Can be used on its own or in combination with entity scanning in the classpath, in both cases avoidingpersistence.xml
.Note that mapping resources must be relative to the classpath root, for example, "META-INF/mappings.xml" or "com/mycompany/repository/mappings.xml", so that they can be loaded through
ClassLoader.getResource
.If no explicit mapping resources have been specified next to
packages to scan
, this manager looks for a defaultMETA-INF/orm.xml
file in the classpath, registering it as a mapping resource for the default unit if the mapping file is not co-located with apersistence.xml
file (in which case we assume it is only meant to be used with the persistence units defined there, like in standard JPA).Note that specifying an empty array/list here suppresses the default
META-INF/orm.xml
check. On the other hand, explicitly specifyingMETA-INF/orm.xml
here will register that file even if it happens to be co-located with apersistence.xml
file. -
setValidationMode
Specify the JPA 2.0 validation mode for all of this manager's persistence units, overriding any value inpersistence.xml
if set.- Since:
- 4.0
- See Also:
-
setDataSources
Specify the JDBC DataSources that the JPA persistence provider is supposed to use for accessing the database, resolving data source names inpersistence.xml
against Spring-managed DataSources.The specified Map needs to define data source names for specific DataSource objects, matching the data source names used in
persistence.xml
. If not specified, data source names will be resolved as JNDI names instead (as defined by standard JPA).- See Also:
-
setDataSourceLookup
Specify the JDBC DataSourceLookup that provides DataSources for the persistence provider, resolving data source names inpersistence.xml
against Spring-managed DataSource instances.Default is JndiDataSourceLookup, which resolves DataSource names as JNDI names (as defined by standard JPA). Specify a BeanFactoryDataSourceLookup instance if you want DataSource names to be resolved against Spring bean names.
Alternatively, consider passing in a map from names to DataSource instances via the "dataSources" property. If the
persistence.xml
file does not define DataSource names at all, specify a default DataSource via the "defaultDataSource" property. -
getDataSourceLookup
Return the JDBC DataSourceLookup that provides DataSources for the persistence provider, resolving data source names inpersistence.xml
against Spring-managed DataSource instances. -
setDefaultDataSource
Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified inpersistence.xml
. This variant indicates no special transaction setup, i.e. typical resource-local.In JPA speak, a DataSource passed in here will be uses as "nonJtaDataSource" on the PersistenceUnitInfo passed to the PersistenceProvider, provided that none has been registered before.
-
getDefaultDataSource
Return the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified inpersistence.xml
. -
setDefaultJtaDataSource
Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified inpersistence.xml
. This variant indicates that JTA is supposed to be used as transaction type.In JPA speak, a DataSource passed in here will be uses as "jtaDataSource" on the PersistenceUnitInfo passed to the PersistenceProvider, provided that none has been registered before.
- See Also:
-
getDefaultJtaDataSource
Return the JTA-aware DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified inpersistence.xml
. -
setPersistenceUnitPostProcessors
public void setPersistenceUnitPostProcessors(@Nullable PersistenceUnitPostProcessor... postProcessors) Set the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager.Such post-processors can, for example, register further entity classes and jar files, in addition to the metadata read from
persistence.xml
. -
getPersistenceUnitPostProcessors
Return the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager. -
setLoadTimeWeaver
Specify the Spring LoadTimeWeaver to use for class instrumentation according to the JPA class transformer contract.It is not required to specify a LoadTimeWeaver: Most providers will be able to provide a subset of their functionality without class instrumentation as well, or operate with their own VM agent specified on JVM startup. Furthermore, DefaultPersistenceUnitManager falls back to an InstrumentationLoadTimeWeaver if Spring's agent-based instrumentation is available at runtime.
In terms of Spring-provided weaving options, the most important ones are InstrumentationLoadTimeWeaver, which requires a Spring-specific (but very general) VM agent specified on JVM startup, and ReflectiveLoadTimeWeaver, which interacts with an underlying ClassLoader based on specific extended methods being available on it (for example, interacting with Spring's TomcatInstrumentableClassLoader). Consider using the
context:load-time-weaver
XML tag for creating such a shared LoadTimeWeaver (autodetecting the environment by default).- Specified by:
setLoadTimeWeaver
in interfaceLoadTimeWeaverAware
- Parameters:
loadTimeWeaver
- theLoadTimeWeaver
instance (nevernull
)- See Also:
-
getLoadTimeWeaver
Return the Spring LoadTimeWeaver to use for class instrumentation according to the JPA class transformer contract. -
setResourceLoader
Description copied from interface:ResourceLoaderAware
Set the ResourceLoader that this object runs in.This might be a ResourcePatternResolver, which can be checked through
instanceof ResourcePatternResolver
. See also theResourcePatternUtils.getResourcePatternResolver
method.Invoked after population of normal bean properties but before an init callback like InitializingBean's
afterPropertiesSet
or a custom init-method. Invoked before ApplicationContextAware'ssetApplicationContext
.- Specified by:
setResourceLoader
in interfaceResourceLoaderAware
- Parameters:
resourceLoader
- the ResourceLoader object to be used by this object- See Also:
-
afterPropertiesSet
public void afterPropertiesSet()Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
preparePersistenceUnitInfos
public void preparePersistenceUnitInfos()Prepare the PersistenceUnitInfos according to the configuration of this manager: scanning forpersistence.xml
files, parsing all matching files, configuring and post-processing them.PersistenceUnitInfos cannot be obtained before this preparation method has been invoked.
-
getPersistenceUnitInfo
@Nullable protected final MutablePersistenceUnitInfo getPersistenceUnitInfo(String persistenceUnitName) Return the specified PersistenceUnitInfo from this manager's cache of processed persistence units, keeping it in the cache (i.e. not 'obtaining' it for use but rather just accessing it for post-processing).This can be used in
postProcessPersistenceUnitInfo(org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo)
implementations, detecting existing persistence units of the same name and potentially merging them.- Parameters:
persistenceUnitName
- the name of the desired persistence unit- Returns:
- the PersistenceUnitInfo in mutable form, or
null
if not available
-
postProcessPersistenceUnitInfo
Hook method allowing subclasses to customize each PersistenceUnitInfo.The default implementation delegates to all registered PersistenceUnitPostProcessors. It is usually preferable to register further entity classes, jar files etc there rather than in a subclass of this manager, to be able to reuse the post-processors.
- Parameters:
pui
- the chosen PersistenceUnitInfo, as read frompersistence.xml
. Passed in as MutablePersistenceUnitInfo.- See Also:
-
isPersistenceUnitOverrideAllowed
protected boolean isPersistenceUnitOverrideAllowed()Return whether an override of a same-named persistence unit is allowed.Default is
false
. May be overridden to returntrue
, for example ifpostProcessPersistenceUnitInfo(org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo)
is able to handle that case. -
obtainDefaultPersistenceUnitInfo
Description copied from interface:PersistenceUnitManager
Obtain the default PersistenceUnitInfo from this manager.- Specified by:
obtainDefaultPersistenceUnitInfo
in interfacePersistenceUnitManager
- Returns:
- the PersistenceUnitInfo (never
null
)
-
obtainPersistenceUnitInfo
Description copied from interface:PersistenceUnitManager
Obtain the specified PersistenceUnitInfo from this manager.- Specified by:
obtainPersistenceUnitInfo
in interfacePersistenceUnitManager
- Parameters:
persistenceUnitName
- the name of the desired persistence unit- Returns:
- the PersistenceUnitInfo (never
null
)
-