Top Description Fields Constructors Methods
org.springframework.orm.jpa.persistenceunit

public Class DefaultPersistenceUnitManager

extends Object
implements PersistenceUnitManager, ResourceLoaderAware, LoadTimeWeaverAware, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.context.weaving.LoadTimeWeaverAware, org.springframework.beans.factory.Aware, org.springframework.context.ResourceLoaderAware, org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager
Imports
java.io.IOException, java.net.URL, java.util.ArrayList, .HashMap, .HashSet, .List, .Map, .Set, javax.sql.DataSource, jakarta.persistence.PersistenceException, .SharedCacheMode, .ValidationMode, jakarta.persistence.spi.PersistenceUnitInfo, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.factory.InitializingBean, org.springframework.context.ResourceLoaderAware, org.springframework.context.weaving.LoadTimeWeaverAware, org.springframework.core.io.Resource, .ResourceLoader, org.springframework.core.io.support.PathMatchingResourcePatternResolver, .ResourcePatternResolver, .ResourcePatternUtils, org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver, .LoadTimeWeaver, org.springframework.jdbc.datasource.lookup.DataSourceLookup, .JndiDataSourceLookup, .MapDataSourceLookup, org.springframework.lang.Nullable, org.springframework.util.ObjectUtils, .ResourceUtils

Default implementation of the PersistenceUnitManager interface. Used as internal default by org.springframework.orm.jpa.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).

Authors
Juergen Hoeller, Stephane Nicoll
Since
2.0
See Also
setPersistenceXmlLocations, setDataSourceLookup, setLoadTimeWeaver, org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean#setPersistenceUnitManager

Field Summary

Modifier and TypeField and Description
private DataSourceLookup
private static final String
public static final String
DEFAULT_PERSISTENCE_XML_LOCATION

Default location of the persistence.xml file: "classpath*:META-INF/persistence.xml".

private DataSource
private DataSource
private String
private String
private LoadTimeWeaver
protected final Log
private ManagedClassNameFilter
private PersistenceManagedTypes
private String[]
public static final String
ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME

Default persistence unit name.

public static final String
ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION

Default location for the persistence unit root URL: "classpath:", indicating the root of the classpath.

private String[]
private static final String
private final Set<String>
private final Map<String, PersistenceUnitInfo>
private PersistenceUnitPostProcessor[]
private String[]
private ResourcePatternResolver
private SharedCacheMode
private ValidationMode

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, 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.

private void
private SpringPersistenceUnitInfo
buildDefaultPersistenceUnitInfo()

Perform Spring-based scanning for entity classes.

private URL

Returns:

the persistence unit root URL to pass to the JPA PersistenceProvider
determineDefaultPersistenceUnitRootUrl
()

Try to determine the persistence unit root URL based on the given "defaultPersistenceUnitRootLocation".

public DataSourceLookup
getDataSourceLookup()

Return the JDBC DataSourceLookup that provides DataSources for the persistence provider, resolving data source names in persistence.xml against Spring-managed DataSource instances.

public DataSource
getDefaultDataSource()

Return the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified in persistence.xml.

public DataSource
getDefaultJtaDataSource()

Return the JTA-aware DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified in persistence.xml.

public LoadTimeWeaver
getLoadTimeWeaver()

Return the Spring LoadTimeWeaver to use for class instrumentation according to the JPA class transformer contract.

private Resource
getOrmXmlForDefaultPersistenceUnit()

Determine JPA's default "META-INF/orm.xml" resource for use with Spring's default persistence unit, if any.

protected final MutablePersistenceUnitInfo

Returns:

the PersistenceUnitInfo in mutable form, or null if not available
getPersistenceUnitInfo
(String
the name of the desired persistence unit
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).

public PersistenceUnitPostProcessor[]
getPersistenceUnitPostProcessors()

Return the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager.

protected boolean
isPersistenceUnitOverrideAllowed()

Return whether an override of a same-named persistence unit is allowed.

public PersistenceUnitInfo
public PersistenceUnitInfo
obtainPersistenceUnitInfo(String
the name of the desired persistence unit
persistenceUnitName
)

Implements org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager.obtainPersistenceUnitInfo.

Obtain the specified PersistenceUnitInfo from this manager.

protected void
postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo
the chosen PersistenceUnitInfo, as read from persistence.xml. Passed in as MutablePersistenceUnitInfo.
pui
)

Hook method allowing subclasses to customize each PersistenceUnitInfo.

public void
preparePersistenceUnitInfos()

Prepare the PersistenceUnitInfos according to the configuration of this manager: scanning for persistence.xml files, parsing all matching files, configuring and post-processing them.

private List<SpringPersistenceUnitInfo>
readPersistenceUnitInfos()

Read all persistence unit infos from persistence.xml, as defined in the JPA specification.

public void
setDataSourceLookup(DataSourceLookup dataSourceLookup)

Specify the JDBC DataSourceLookup that provides DataSources for the persistence provider, resolving data source names in persistence.xml against Spring-managed DataSource instances.

public 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 in persistence.xml against Spring-managed DataSources.

public 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 in persistence.xml.

public 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 in persistence.xml.

public void
setDefaultPersistenceUnitName(String defaultPersistenceUnitName)

Specify the name of the default persistence unit, if any.

public void
setDefaultPersistenceUnitRootLocation(String defaultPersistenceUnitRootLocation)

Set the default persistence unit root location, to be applied if no unit-specific persistence unit root could be determined.

public void
setLoadTimeWeaver(LoadTimeWeaver
the LoadTimeWeaver instance (never null)
loadTimeWeaver
)

Implements org.springframework.context.weaving.LoadTimeWeaverAware.setLoadTimeWeaver.

Specify the Spring LoadTimeWeaver to use for class instrumentation according to the JPA class transformer contract.

public void
setManagedClassNameFilter(ManagedClassNameFilter
a predicate to filter entity classes
managedClassNameFilter
)

Set the ManagedClassNameFilter to apply on entity classes discovered using classpath scanning.

public void
setManagedTypes(PersistenceManagedTypes
the managed types
managedTypes
)

Set the PersistenceManagedTypes to use to build the list of managed types as an alternative to entity scanning.

public void
setMappingResources(String... mappingResources)

Specify one or more mapping resources (equivalent to <mapping-file> entries in persistence.xml) for the default persistence unit.

public 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 with persistence.xml markers in them.

public void
setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors)

Set the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager.

public void
setPersistenceXmlLocation(String persistenceXmlLocation)

Specify the location of the persistence.xml files to load.

public void
setPersistenceXmlLocations(String...
an array of Spring resource Strings identifying the location of the persistence.xml files to read
persistenceXmlLocations
)

Specify multiple locations of persistence.xml files to load.

public void
setResourceLoader(ResourceLoader
the ResourceLoader object to be used by this object
resourceLoader
)

Implements org.springframework.context.ResourceLoaderAware.setResourceLoader.

Set the ResourceLoader that this object runs in.

This might be a ResourcePatternResolver, which can be checked through instanceof ResourcePatternResolver.

public void
setSharedCacheMode(SharedCacheMode sharedCacheMode)

Specify the JPA 2.0 shared cache mode for all of this manager's persistence units, overriding any value in persistence.xml if set.

public void
setValidationMode(ValidationMode validationMode)

Specify the JPA 2.0 validation mode for all of this manager's persistence units, overriding any value in persistence.xml if set.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dataSourceLookupback to summary
private DataSourceLookup dataSourceLookup
DEFAULT_ORM_XML_RESOURCEback to summary
private static final String DEFAULT_ORM_XML_RESOURCE
DEFAULT_PERSISTENCE_XML_LOCATIONback to summary
public static final String DEFAULT_PERSISTENCE_XML_LOCATION

Default location of the persistence.xml file: "classpath*:META-INF/persistence.xml".

defaultDataSourceback to summary
private DataSource defaultDataSource
Annotations
@Nullable
defaultJtaDataSourceback to summary
private DataSource defaultJtaDataSource
Annotations
@Nullable
defaultPersistenceUnitNameback to summary
private String defaultPersistenceUnitName
Annotations
@Nullable
defaultPersistenceUnitRootLocationback to summary
private String defaultPersistenceUnitRootLocation
Annotations
@Nullable
loadTimeWeaverback to summary
private LoadTimeWeaver loadTimeWeaver
Annotations
@Nullable
loggerback to summary
protected final Log logger
managedClassNameFilterback to summary
private ManagedClassNameFilter managedClassNameFilter
Annotations
@Nullable
managedTypesback to summary
private PersistenceManagedTypes managedTypes
Annotations
@Nullable
mappingResourcesback to summary
private String[] mappingResources
Annotations
@Nullable
ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAMEback to summary
public static final String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME

Default persistence unit name.

ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATIONback to summary
public static final String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION

Default location for the persistence unit root URL: "classpath:", indicating the root of the classpath.

packagesToScanback to summary
private String[] packagesToScan
Annotations
@Nullable
PERSISTENCE_XML_FILENAMEback to summary
private static final String PERSISTENCE_XML_FILENAME
persistenceUnitInfoNamesback to summary
private final Set<String> persistenceUnitInfoNames
persistenceUnitInfosback to summary
private final Map<String, PersistenceUnitInfo> persistenceUnitInfos
persistenceUnitPostProcessorsback to summary
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors
Annotations
@Nullable
persistenceXmlLocationsback to summary
private String[] persistenceXmlLocations
resourcePatternResolverback to summary
private ResourcePatternResolver resourcePatternResolver
sharedCacheModeback to summary
private SharedCacheMode sharedCacheMode
Annotations
@Nullable
validationModeback to summary
private ValidationMode validationMode
Annotations
@Nullable

Constructor Detail

DefaultPersistenceUnitManagerback to summary
public DefaultPersistenceUnitManager()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, 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.

Annotations
@Override
applyManagedTypesback to summary
private void applyManagedTypes(SpringPersistenceUnitInfo scannedUnit, PersistenceManagedTypes managedTypes)
buildDefaultPersistenceUnitInfoback to summary
private SpringPersistenceUnitInfo buildDefaultPersistenceUnitInfo()

Perform Spring-based scanning for entity classes.

See Also
setPackagesToScan
determineDefaultPersistenceUnitRootUrlback to summary
private URL determineDefaultPersistenceUnitRootUrl()

Try to determine the persistence unit root URL based on the given "defaultPersistenceUnitRootLocation".

Returns:URL

the persistence unit root URL to pass to the JPA PersistenceProvider

Annotations
@Nullable
See Also
setDefaultPersistenceUnitRootLocation
getDataSourceLookupback to summary
public DataSourceLookup getDataSourceLookup()

Return the JDBC DataSourceLookup that provides DataSources for the persistence provider, resolving data source names in persistence.xml against Spring-managed DataSource instances.

Annotations
@Nullable
getDefaultDataSourceback to summary
public DataSource getDefaultDataSource()

Return the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified in persistence.xml.

Annotations
@Nullable
getDefaultJtaDataSourceback to summary
public DataSource getDefaultJtaDataSource()

Return the JTA-aware DataSource that the JPA persistence provider is supposed to use for accessing the database if none has been specified in persistence.xml.

Annotations
@Nullable
getLoadTimeWeaverback to summary
public LoadTimeWeaver getLoadTimeWeaver()

Return the Spring LoadTimeWeaver to use for class instrumentation according to the JPA class transformer contract.

Annotations
@Nullable
getOrmXmlForDefaultPersistenceUnitback to summary
private Resource getOrmXmlForDefaultPersistenceUnit()

Determine JPA's default "META-INF/orm.xml" resource for use with Spring's default persistence unit, if any.

Checks whether a "META-INF/orm.xml" file exists in the classpath and uses it if it is not co-located with a "META-INF/persistence.xml" file.

Annotations
@Nullable
getPersistenceUnitInfoback to summary
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 implementations, detecting existing persistence units of the same name and potentially merging them.

Parameters
persistenceUnitName:String

the name of the desired persistence unit

Returns:MutablePersistenceUnitInfo

the PersistenceUnitInfo in mutable form, or null if not available

Annotations
@Nullable
getPersistenceUnitPostProcessorsback to summary
public PersistenceUnitPostProcessor[] getPersistenceUnitPostProcessors()

Return the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager.

Annotations
@Nullable
isPersistenceUnitOverrideAllowedback to summary
protected boolean isPersistenceUnitOverrideAllowed()

Return whether an override of a same-named persistence unit is allowed.

Default is false. May be overridden to return true, for example if postProcessPersistenceUnitInfo is able to handle that case.

obtainDefaultPersistenceUnitInfoback to summary
public PersistenceUnitInfo obtainDefaultPersistenceUnitInfo()

Implements org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager.obtainDefaultPersistenceUnitInfo.

Doc from org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager.obtainDefaultPersistenceUnitInfo.

Obtain the default PersistenceUnitInfo from this manager.

Returns:PersistenceUnitInfo

the PersistenceUnitInfo (never null)

Annotations
@Override
obtainPersistenceUnitInfoback to summary
public PersistenceUnitInfo obtainPersistenceUnitInfo(String persistenceUnitName)

Implements org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager.obtainPersistenceUnitInfo.

Doc from org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager.obtainPersistenceUnitInfo.

Obtain the specified PersistenceUnitInfo from this manager.

Parameters
persistenceUnitName:String

the name of the desired persistence unit

Returns:PersistenceUnitInfo

the PersistenceUnitInfo (never null)

Annotations
@Override
postProcessPersistenceUnitInfoback to summary
protected void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui)

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:MutablePersistenceUnitInfo

the chosen PersistenceUnitInfo, as read from persistence.xml. Passed in as MutablePersistenceUnitInfo.

See Also
setPersistenceUnitPostProcessors
preparePersistenceUnitInfosback to summary
public void preparePersistenceUnitInfos()

Prepare the PersistenceUnitInfos according to the configuration of this manager: scanning for persistence.xml files, parsing all matching files, configuring and post-processing them.

PersistenceUnitInfos cannot be obtained before this preparation method has been invoked.

See Also
obtainDefaultPersistenceUnitInfo(), obtainPersistenceUnitInfo(String)
readPersistenceUnitInfosback to summary
private List<SpringPersistenceUnitInfo> readPersistenceUnitInfos()

Read all persistence unit infos from persistence.xml, as defined in the JPA specification.

setDataSourceLookupback to summary
public void setDataSourceLookup(DataSourceLookup dataSourceLookup)

Specify the JDBC DataSourceLookup that provides DataSources for the persistence provider, resolving data source names in persistence.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.

See Also
org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup, org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup, setDataSources, setDefaultDataSource
setDataSourcesback to summary
public 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 in persistence.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
org.springframework.jdbc.datasource.lookup.MapDataSourceLookup
setDefaultDataSourceback to summary
public 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 in persistence.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.

See Also
jakarta.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
setDefaultJtaDataSourceback to summary
public 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 in persistence.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
jakarta.persistence.spi.PersistenceUnitInfo#getJtaDataSource()
setDefaultPersistenceUnitNameback to summary
public void setDefaultPersistenceUnitName(String defaultPersistenceUnitName)

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.

See Also
setPackagesToScan, obtainDefaultPersistenceUnitInfo
setDefaultPersistenceUnitRootLocationback to summary
public void setDefaultPersistenceUnitRootLocation(String defaultPersistenceUnitRootLocation)

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.

setLoadTimeWeaverback to summary
public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver)

Implements org.springframework.context.weaving.LoadTimeWeaverAware.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).

Parameters
loadTimeWeaver:LoadTimeWeaver

Doc from org.springframework.context.weaving.LoadTimeWeaverAware.setLoadTimeWeaver.

the LoadTimeWeaver instance (never null)

Annotations
@Override
See Also
org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver, org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver
setManagedClassNameFilterback to summary
public void setManagedClassNameFilter(ManagedClassNameFilter managedClassNameFilter)

Set the ManagedClassNameFilter to apply on entity classes discovered using classpath scanning.

Parameters
managedClassNameFilter:ManagedClassNameFilter

a predicate to filter entity classes

Since
6.1.4
setManagedTypesback to summary
public void setManagedTypes(PersistenceManagedTypes managedTypes)

Set the PersistenceManagedTypes to use to build the list of managed types as an alternative to entity scanning.

Parameters
managedTypes:PersistenceManagedTypes

the managed types

Since
6.0
setMappingResourcesback to summary
public void setMappingResources(String... mappingResources)

Specify one or more mapping resources (equivalent to <mapping-file> entries in persistence.xml) for the default persistence unit. Can be used on its own or in combination with entity scanning in the classpath, in both cases avoiding persistence.xml.

Note that mapping resources must be relative to the classpath root, e.g. "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 default META-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 a persistence.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 specifying META-INF/orm.xml here will register that file even if it happens to be co-located with a persistence.xml file.

See Also
setDefaultPersistenceUnitName, setPackagesToScan
setPackagesToScanback to summary
public 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 with persistence.xml markers in them. In case of Spring-based scanning, no persistence.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 (org.springframework.context.annotation.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 by setDefaultPersistenceUnitName: 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 given org.springframework.orm.jpa.JpaVendorAdapter (e.g. for Hibernate).

If no explicit mapping resources have been specified in addition to these packages, this manager looks for a default META-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 a persistence.xml file (in which case we assume it is only meant to be used with the persistence units defined there, like in standard JPA).

See Also
setManagedClassNameFilter(ManagedClassNameFilter), setManagedTypes(PersistenceManagedTypes), setDefaultPersistenceUnitName, setMappingResources
setPersistenceUnitPostProcessorsback to summary
public void setPersistenceUnitPostProcessors(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.

setPersistenceXmlLocationback to summary
public void setPersistenceXmlLocation(String persistenceXmlLocation)

Specify the location of the persistence.xml files to load. These can be specified as Spring resource locations and/or location patterns.

Default is "classpath*:META-INF/persistence.xml".

setPersistenceXmlLocationsback to summary
public void setPersistenceXmlLocations(String... persistenceXmlLocations)

Specify multiple locations of persistence.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:String[]

an array of Spring resource Strings identifying the location of the persistence.xml files to read

setResourceLoaderback to summary
public void setResourceLoader(ResourceLoader resourceLoader)

Implements org.springframework.context.ResourceLoaderAware.setResourceLoader.

Doc from org.springframework.context.ResourceLoaderAware.setResourceLoader.

Set the ResourceLoader that this object runs in.

This might be a ResourcePatternResolver, which can be checked through instanceof ResourcePatternResolver. See also the ResourcePatternUtils.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's setApplicationContext.

Parameters
resourceLoader:ResourceLoader

the ResourceLoader object to be used by this object

Annotations
@Override
setSharedCacheModeback to summary
public void setSharedCacheMode(SharedCacheMode sharedCacheMode)

Specify the JPA 2.0 shared cache mode for all of this manager's persistence units, overriding any value in persistence.xml if set.

Since
4.0
See Also
jakarta.persistence.spi.PersistenceUnitInfo#getSharedCacheMode()
setValidationModeback to summary
public void setValidationMode(ValidationMode validationMode)

Specify the JPA 2.0 validation mode for all of this manager's persistence units, overriding any value in persistence.xml if set.

Since
4.0
See Also
jakarta.persistence.spi.PersistenceUnitInfo#getValidationMode()