org.springframework.orm.jpa.persistenceunit
Class DefaultPersistenceUnitManager

java.lang.Object
  extended by org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager
All Implemented Interfaces:
Aware, InitializingBean, ResourceLoaderAware, LoadTimeWeaverAware, PersistenceUnitManager

public class DefaultPersistenceUnitManager
extends java.lang.Object
implements PersistenceUnitManager, ResourceLoaderAware, LoadTimeWeaverAware, InitializingBean

Default implementation of the 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.

The default XML file location is classpath*:META-INF/persistence.xml, scanning for all matching files in the class path (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
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)

Nested Class Summary
private static class DefaultPersistenceUnitManager.Jpa2PersistenceUnitInfoDecorator
          Decorator that exposes a JPA 2.0 compliant PersistenceUnitInfo interface for a JPA 1.0 based SpringPersistenceUnitInfo object, adapting the getSharedCacheMode and getValidationMode methods from String names to enum return values.
 
Field Summary
private  DataSourceLookup dataSourceLookup
           
static java.lang.String DEFAULT_PERSISTENCE_XML_LOCATION
          Default location of the persistence.xml file: "classpath*:META-INF/persistence.xml".
private  javax.sql.DataSource defaultDataSource
           
private  java.lang.String defaultPersistenceUnitName
           
private  java.lang.String defaultPersistenceUnitRootLocation
           
private static TypeFilter[] entityTypeFilters
           
private static boolean jpa2ApiPresent
           
private  LoadTimeWeaver loadTimeWeaver
           
static java.lang.String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME
           
static java.lang.String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION
          Default location for the persistence unit root URL: "classpath:", indicating the root of the class path.
private  java.lang.String[] packagesToScan
           
private  java.util.Set<java.lang.String> persistenceUnitInfoNames
           
private  java.util.Map<java.lang.String,PersistenceUnitInfo> persistenceUnitInfos
           
private  PersistenceUnitPostProcessor[] persistenceUnitPostProcessors
           
private  java.lang.String[] persistenceXmlLocations
           
private static java.lang.String RESOURCE_PATTERN
           
private  ResourcePatternResolver resourcePatternResolver
           
 
Constructor Summary
DefaultPersistenceUnitManager()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
private  java.net.URL determineDefaultPersistenceUnitRootUrl()
          Try to determine the persistence unit root URL based on the given "defaultPersistenceUnitRootLocation".
 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.
 javax.sql.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.
 LoadTimeWeaver getLoadTimeWeaver()
          Return the Spring LoadTimeWeaver to use for class instrumentation according to the JPA class transformer contract.
protected  MutablePersistenceUnitInfo getPersistenceUnitInfo(java.lang.String persistenceUnitName)
          Return the specified PersistenceUnitInfo from this manager's cache of processed persistence units, keeping it in the cache (i.e.
 PersistenceUnitPostProcessor[] getPersistenceUnitPostProcessors()
          Return the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager.
private  boolean matchesFilter(MetadataReader reader, MetadataReaderFactory readerFactory)
          Check whether any of the configured entity type filters matches the current class descriptor contained in the metadata reader.
 PersistenceUnitInfo obtainDefaultPersistenceUnitInfo()
          Obtain the default PersistenceUnitInfo from this manager.
 PersistenceUnitInfo obtainPersistenceUnitInfo(java.lang.String persistenceUnitName)
          Obtain the specified PersistenceUnitInfo from this manager.
protected  void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui)
          Hook method allowing subclasses to customize each PersistenceUnitInfo.
 void preparePersistenceUnitInfos()
          Prepare the PersistenceUnitInfos according to the configuration of this manager: scanning for persistence.xml files, parsing all matching files, configurating and post-processing them.
private  java.util.List<SpringPersistenceUnitInfo> readPersistenceUnitInfos()
          Read all persistence unit infos from persistence.xml, as defined in the JPA specification.
private  SpringPersistenceUnitInfo scanPackages()
          Perform Spring-based scanning for entity classes.
 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.
 void setDataSources(java.util.Map<java.lang.String,javax.sql.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.
 void setDefaultDataSource(javax.sql.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.
 void setDefaultPersistenceUnitName(java.lang.String defaultPersistenceUnitName)
          Specify the name of the default persistence unit, if any.
 void setDefaultPersistenceUnitRootLocation(java.lang.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 setPackagesToScan(java.lang.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.
 void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors)
          Set the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager.
 void setPersistenceXmlLocation(java.lang.String persistenceXmlLocation)
          Specify the location of the persistence.xml files to load.
 void setPersistenceXmlLocations(java.lang.String... persistenceXmlLocations)
          Specify multiple locations of persistence.xml files to load.
 void setResourceLoader(ResourceLoader resourceLoader)
          Set the ResourceLoader that this object runs in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PERSISTENCE_XML_LOCATION

public static final java.lang.String DEFAULT_PERSISTENCE_XML_LOCATION
Default location of the persistence.xml file: "classpath*:META-INF/persistence.xml".

See Also:
Constant Field Values

ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION

public static final java.lang.String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_ROOT_LOCATION
Default location for the persistence unit root URL: "classpath:", indicating the root of the class path.

See Also:
Constant Field Values

ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME

public static final java.lang.String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME
See Also:
Constant Field Values

RESOURCE_PATTERN

private static final java.lang.String RESOURCE_PATTERN
See Also:
Constant Field Values

jpa2ApiPresent

private static final boolean jpa2ApiPresent

entityTypeFilters

private static final TypeFilter[] entityTypeFilters

persistenceXmlLocations

private java.lang.String[] persistenceXmlLocations

defaultPersistenceUnitRootLocation

private java.lang.String defaultPersistenceUnitRootLocation

defaultPersistenceUnitName

private java.lang.String defaultPersistenceUnitName

packagesToScan

private java.lang.String[] packagesToScan

dataSourceLookup

private DataSourceLookup dataSourceLookup

defaultDataSource

private javax.sql.DataSource defaultDataSource

persistenceUnitPostProcessors

private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors

loadTimeWeaver

private LoadTimeWeaver loadTimeWeaver

resourcePatternResolver

private ResourcePatternResolver resourcePatternResolver

persistenceUnitInfoNames

private final java.util.Set<java.lang.String> persistenceUnitInfoNames

persistenceUnitInfos

private final java.util.Map<java.lang.String,PersistenceUnitInfo> persistenceUnitInfos
Constructor Detail

DefaultPersistenceUnitManager

public DefaultPersistenceUnitManager()
Method Detail

setPersistenceXmlLocation

public void setPersistenceXmlLocation(java.lang.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".


setPersistenceXmlLocations

public void setPersistenceXmlLocations(java.lang.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 - an array of Spring resource Strings identifying the location of the persistence.xml files to read

setDefaultPersistenceUnitRootLocation

public void setDefaultPersistenceUnitRootLocation(java.lang.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 class path (nearest root directory). To be overridden if unit-specific resolution does not work and the class path root is not appropriate either.


setDefaultPersistenceUnitName

public void setDefaultPersistenceUnitName(java.lang.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(java.lang.String...), obtainDefaultPersistenceUnitInfo()

setPackagesToScan

public void setPackagesToScan(java.lang.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 (ClassPathBeanDefinitionScanner).


setDataSources

public void setDataSources(java.util.Map<java.lang.String,javax.sql.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:
MapDataSourceLookup

setDataSourceLookup

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:
JndiDataSourceLookup, BeanFactoryDataSourceLookup, setDataSources(java.util.Map), setDefaultDataSource(javax.sql.DataSource)

getDataSourceLookup

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.


setDefaultDataSource

public void setDefaultDataSource(javax.sql.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.

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:
javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()

getDefaultDataSource

public javax.sql.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.


setPersistenceUnitPostProcessors

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


getPersistenceUnitPostProcessors

public PersistenceUnitPostProcessor[] getPersistenceUnitPostProcessors()
Return the PersistenceUnitPostProcessors to be applied to each PersistenceUnitInfo that has been parsed by this manager.


setLoadTimeWeaver

public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver)
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 VM agent specified on JVM startup.

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).

NOTE: As of Spring 2.5, the context's default LoadTimeWeaver (defined as bean with name "loadTimeWeaver") will be picked up automatically, if available, removing the need for LoadTimeWeaver configuration on each affected target bean. Consider using the context:load-time-weaver XML tag for creating such a shared LoadTimeWeaver (autodetecting the environment by default).

Specified by:
setLoadTimeWeaver in interface LoadTimeWeaverAware
Parameters:
loadTimeWeaver - the LoadTimeWeaver instance (never null)
See Also:
InstrumentationLoadTimeWeaver, ReflectiveLoadTimeWeaver, TomcatInstrumentableClassLoader

getLoadTimeWeaver

public LoadTimeWeaver getLoadTimeWeaver()
Return the Spring LoadTimeWeaver to use for class instrumentation according to the JPA class transformer contract.


setResourceLoader

public void setResourceLoader(ResourceLoader resourceLoader)
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 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.

Specified by:
setResourceLoader in interface ResourceLoaderAware
Parameters:
resourceLoader - ResourceLoader object to be used by this object
See Also:
ResourcePatternResolver, ResourcePatternUtils.getResourcePatternResolver(org.springframework.core.io.ResourceLoader)

afterPropertiesSet

public void afterPropertiesSet()
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean

preparePersistenceUnitInfos

public void preparePersistenceUnitInfos()
Prepare the PersistenceUnitInfos according to the configuration of this manager: scanning for persistence.xml files, parsing all matching files, configurating and post-processing them.

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

See Also:
obtainDefaultPersistenceUnitInfo(), obtainPersistenceUnitInfo(String)

readPersistenceUnitInfos

private java.util.List<SpringPersistenceUnitInfo> readPersistenceUnitInfos()
Read all persistence unit infos from persistence.xml, as defined in the JPA specification.


scanPackages

private SpringPersistenceUnitInfo scanPackages()
Perform Spring-based scanning for entity classes.

See Also:
setPackagesToScan(java.lang.String...)

matchesFilter

private boolean matchesFilter(MetadataReader reader,
                              MetadataReaderFactory readerFactory)
                       throws java.io.IOException
Check whether any of the configured entity type filters matches the current class descriptor contained in the metadata reader.

Throws:
java.io.IOException

determineDefaultPersistenceUnitRootUrl

private java.net.URL determineDefaultPersistenceUnitRootUrl()
Try to determine the persistence unit root URL based on the given "defaultPersistenceUnitRootLocation".

Returns:
the persistence unit root URL to pass to the JPA PersistenceProvider
See Also:
setDefaultPersistenceUnitRootLocation(java.lang.String)

getPersistenceUnitInfo

protected final MutablePersistenceUnitInfo getPersistenceUnitInfo(java.lang.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

protected void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui)
Hook method allowing subclasses to customize each PersistenceUnitInfo.

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 from persistence.xml. Passed in as MutablePersistenceUnitInfo.
See Also:
setPersistenceUnitPostProcessors(org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor...)

obtainDefaultPersistenceUnitInfo

public PersistenceUnitInfo obtainDefaultPersistenceUnitInfo()
Description copied from interface: PersistenceUnitManager
Obtain the default PersistenceUnitInfo from this manager.

Specified by:
obtainDefaultPersistenceUnitInfo in interface PersistenceUnitManager
Returns:
the PersistenceUnitInfo (never null)

obtainPersistenceUnitInfo

public PersistenceUnitInfo obtainPersistenceUnitInfo(java.lang.String persistenceUnitName)
Description copied from interface: PersistenceUnitManager
Obtain the specified PersistenceUnitInfo from this manager.

Specified by:
obtainPersistenceUnitInfo in interface PersistenceUnitManager
Parameters:
persistenceUnitName - the name of the desired persistence unit
Returns:
the PersistenceUnitInfo (never null)