public class KradEntityManagerFactoryBean extends Object implements org.springframework.beans.factory.FactoryBean<javax.persistence.EntityManagerFactory>, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean, org.springframework.orm.jpa.EntityManagerFactoryInfo, org.springframework.dao.support.PersistenceExceptionTranslator, org.springframework.context.ResourceLoaderAware, org.springframework.context.weaving.LoadTimeWeaverAware
EntityManagerFactory
factory bean which can be used to configure a JPA
persistence unit using the Spring Framework.
This implementation does not support the use of a custom PersistenceUnitManager, but rather stores and manages one
internally. This is intended to be an alternative to direct usage of Spring's
LocalContainerEntityManagerFactoryBean
in order to make JPA configuration with KRAD simpler.
Minimal configuration of this factory bean will include the following:
persistenceUnitName
dataSource
or jtaDataSource
persistenceProvider
or jpaVendorAdapter
Note that persistence unit names must be unique, so choose a name which is unlikely to clash with any potential persistence units configured within the runtime environment.
When leveraging this class, persistence.xml files are not used. Rather, persistence unit configuration is loaded
via the various settings provided on this factory bean which contain everything needed to create the desired
persistence unit configuration in the majority of cases. If a KRAD application needs more control over the
configuration of the persistence unit or wants to use persistence.xml and JPA's default bootstrapping and classpath
scanning behavior, an EntityManagerFactory can be configured using the other classes provided by the Spring
framework or by other means as needed. See LocalContainerEntityManagerFactoryBean
for an alternative
approach.
Only one of JTA or non-JTA datasource can be set. Depending on which one is set, the underlying persistence unit
will have it's PersistenceUnitTransactionType
set to either RESOURCE_LOCAL or JTA. If
both of these are set, then this class will throw an IllegalStateException
when the
afterPropertiesSet()
method is invoked by the Spring Framework.
Elsewhere, this class delegates to implementations of LocalContainerEntityManagerFactoryBean
and
DefaultPersistenceUnitManager
, so information on the specific behavior of some of the settings and methods on
this class can be found on the javadoc for those classes as well.
When afterPropertiesSet()
is invoked, this class will scan the current ConfigContext
for JPA
properties and make them available to the persistence unit. It will combine these with any properties that were set
directly on this factory bean via the setJpaProperties(java.util.Properties)
or
setJpaPropertyMap(java.util.Map)
methods.
loadCustomJpaDefaults(java.util.Map)
to allow for possible subclass customization of JPA
property defaultssetJpaPropertyMap(java.util.Map)
and
setJpaProperties(java.util.Properties)
. It is potentially non-deterministic which of these setters will
take precedence over the other, so it is recommended to only invoke one of them on a given instance of this
factory bean.This class can be subclassed to provide additional specialized implementations of this factory bean. A potential use for this might be to provide a factory bean that defaults certain values as part of it's default setup.
Modifier and Type | Field and Description |
---|---|
static String |
GLOBAL_JPA_PROPERTY_PREFIX
Prefix for property names that are passed to *all* JPA persistence contexts as JPA properties.
|
static String |
JPA_PROPERTY_PREFIX
Prefix for property names that are passed to the JPA persistence context as JPA properties.
|
Constructor and Description |
---|
KradEntityManagerFactoryBean()
Creates a default KRAD-managed factory bean.
|
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor[] |
assemblePersistenceUnitPostProcessors()
Assembles the
PersistenceUnitPostProcessor s into an array. |
protected String |
constructPersistenceUnitJpaPropertyPrefix()
Builds a persistence unit JPA property prefix.
|
protected org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean |
createInternalFactoryBean(org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager manager)
Creates a JPA-specific entity manager factory bean.
|
protected org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager |
createPersistenceUnitManager()
Creates a persistence unit manager.
|
void |
destroy() |
ClassLoader |
getBeanClassLoader() |
DataSource |
getDataSource() |
Class<? extends javax.persistence.EntityManager> |
getEntityManagerInterface() |
protected org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean |
getInternalFactoryBean()
Retrieve a reference to the internal
LocalContainerEntityManagerFactoryBean which is used by this factory
bean. |
org.springframework.orm.jpa.JpaDialect |
getJpaDialect() |
Map<String,Object> |
getJpaPropertyMap()
Allow Map access to the JPA properties to be passed to the persistence provider, with the option to add or
override specific entries.
|
List<String> |
getManagedClassNames()
Returns the list of all managed class names which have been configured on this factory bean.
|
javax.persistence.EntityManagerFactory |
getNativeEntityManagerFactory() |
javax.persistence.EntityManagerFactory |
getObject() |
Class<? extends javax.persistence.EntityManagerFactory> |
getObjectType() |
javax.persistence.spi.PersistenceProvider |
getPersistenceProvider() |
javax.persistence.spi.PersistenceUnitInfo |
getPersistenceUnitInfo() |
protected org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager |
getPersistenceUnitManager()
Returns a reference to the internal
DefaultPersistenceUnitManager which is used by this factory bean. |
String |
getPersistenceUnitName() |
org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor[] |
getPersistenceUnitPostProcessors()
Returns an array of the
PersistenceUnitPostProcessor instances which have been configured on this
factory bean. |
boolean |
isSingleton() |
protected void |
loadCustomJpaDefaults(Map<String,String> jpaProperties)
Allows for loading of custom JPA defaults by subclasses, default implementation does nothing so subclasses need
not call super.loadCustomJpaDefaults.
|
protected void |
loadGlobalJpaDefaults(Map<String,String> jpaProperties)
Loads the global JPA defaults from the config.
|
protected void |
loadPersistenceUnitJpaDefaults(Map<String,String> jpaProperties)
Loads the persistence unit JPA defaults from the config.
|
void |
setBeanClassLoader(ClassLoader classLoader) |
void |
setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) |
void |
setBeanName(String name) |
void |
setDataSource(DataSource dataSource)
Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database.
|
void |
setEntityManagerFactoryInterface(Class<? extends javax.persistence.EntityManagerFactory> emfInterface)
Specify the (potentially vendor-specific) EntityManagerFactory interface that this EntityManagerFactory proxy is
supposed to implement.
|
void |
setEntityManagerInterface(Class<? extends javax.persistence.EntityManager> emInterface)
Specify the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers are supposed
to implement.
|
void |
setJpaDialect(org.springframework.orm.jpa.JpaDialect jpaDialect)
Specify the vendor-specific JpaDialect implementation to associate with this EntityManagerFactory.
|
void |
setJpaProperties(Properties jpaProperties)
Specify JPA properties, to be passed into
Persistence.createEntityManagerFactory (if any). |
void |
setJpaPropertyMap(Map<String,?> jpaProperties)
Specify JPA properties as a Map, to be passed into
Persistence.createEntityManagerFactory (if any). |
void |
setJpaVendorAdapter(org.springframework.orm.jpa.JpaVendorAdapter jpaVendorAdapter)
Specify the JpaVendorAdapter implementation for the desired JPA provider, if any.
|
void |
setJtaDataSource(DataSource jtaDataSource)
Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database.
|
void |
setLoadTimeWeaver(org.springframework.instrument.classloading.LoadTimeWeaver loadTimeWeaver) |
void |
setManagedClassNames(List<String> managedClassNames)
Specifies a List of class names which should be loaded into the resulting EntityManagerFactory as managed
JPA classes.
|
void |
setMappingResources(String... mappingResources)
Specify one or more mapping resources (equivalent to
<mapping-file> entries in
persistence.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 with
persistence.xml markers in them. |
void |
setPersistenceProvider(javax.persistence.spi.PersistenceProvider persistenceProvider)
Set the PersistenceProvider instance to use for creating the EntityManagerFactory.
|
void |
setPersistenceUnitName(String persistenceUnitName)
Specify the name of the EntityManagerFactory configuration.
|
void |
setPersistenceUnitPostProcessors(org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor... postProcessors)
Set the PersistenceUnitPostProcessors to be applied to the * PersistenceUnitInfo used for creating this
EntityManagerFactory.
|
void |
setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader) |
org.springframework.dao.DataAccessException |
translateExceptionIfPossible(RuntimeException ex) |
public static final String JPA_PROPERTY_PREFIX
To use this, concatenate this prefix with the persistence context name. The total prefix (including the
persistence context) will then be stripped before being passed to the JPA entity manager factory when using
the KradEntityManagerFactoryBean
.
public static final String GLOBAL_JPA_PROPERTY_PREFIX
The total prefix will then be stripped before being passed to all JPA entity manager factories that use the
KradEntityManagerFactoryBean
.
public KradEntityManagerFactoryBean()
protected org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean getInternalFactoryBean()
LocalContainerEntityManagerFactoryBean
which is used by this factory
bean.
Primarily intended to allow subclasses to access this internal factory bean when needed.
LocalContainerEntityManagerFactoryBean
managed by this beanprotected org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager createPersistenceUnitManager()
protected org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createInternalFactoryBean(org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager manager)
manager
- the persistence unit manager to use.public void afterPropertiesSet() throws javax.persistence.PersistenceException
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
javax.persistence.PersistenceException
protected void loadCustomJpaDefaults(Map<String,String> jpaProperties)
Subclasses are free to override this method as they see fit. This method is executed after other defaults are loaded. A reference to the current Map of JPA properties is passed. Subclasses should take care if removing or overwriting any of the values which already exist in the given Map.
jpaProperties
- the current Map of JPA property defaults.protected void loadGlobalJpaDefaults(Map<String,String> jpaProperties)
jpaProperties
- the current Map of JPA property defaults.protected void loadPersistenceUnitJpaDefaults(Map<String,String> jpaProperties)
jpaProperties
- the current Map of JPA property defaults.protected String constructPersistenceUnitJpaPropertyPrefix()
protected org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor[] assemblePersistenceUnitPostProcessors()
PersistenceUnitPostProcessor
s into an array.PersistenceUnitPostProcessor
s.public List<String> getManagedClassNames()
This list is modifiable, so the returned list may be modified directly if desired.
public org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor[] getPersistenceUnitPostProcessors()
PersistenceUnitPostProcessor
instances which have been configured on this
factory bean.protected org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager getPersistenceUnitManager()
DefaultPersistenceUnitManager
which is used by this factory bean.public void destroy()
destroy
in interface org.springframework.beans.factory.DisposableBean
public Class<? extends javax.persistence.EntityManagerFactory> getObjectType()
getObjectType
in interface org.springframework.beans.factory.FactoryBean<javax.persistence.EntityManagerFactory>
public boolean isSingleton()
isSingleton
in interface org.springframework.beans.factory.FactoryBean<javax.persistence.EntityManagerFactory>
public javax.persistence.EntityManagerFactory getObject()
getObject
in interface org.springframework.beans.factory.FactoryBean<javax.persistence.EntityManagerFactory>
public javax.persistence.EntityManagerFactory getNativeEntityManagerFactory()
getNativeEntityManagerFactory
in interface org.springframework.orm.jpa.EntityManagerFactoryInfo
public void setBeanName(String name)
setBeanName
in interface org.springframework.beans.factory.BeanNameAware
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
setBeanFactory
in interface org.springframework.beans.factory.BeanFactoryAware
public ClassLoader getBeanClassLoader()
getBeanClassLoader
in interface org.springframework.orm.jpa.EntityManagerFactoryInfo
public void setBeanClassLoader(ClassLoader classLoader)
setBeanClassLoader
in interface org.springframework.beans.factory.BeanClassLoaderAware
public Class<? extends javax.persistence.EntityManager> getEntityManagerInterface()
getEntityManagerInterface
in interface org.springframework.orm.jpa.EntityManagerFactoryInfo
public void setLoadTimeWeaver(org.springframework.instrument.classloading.LoadTimeWeaver loadTimeWeaver)
setLoadTimeWeaver
in interface org.springframework.context.weaving.LoadTimeWeaverAware
public void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
setResourceLoader
in interface org.springframework.context.ResourceLoaderAware
public javax.persistence.spi.PersistenceUnitInfo getPersistenceUnitInfo()
getPersistenceUnitInfo
in interface org.springframework.orm.jpa.EntityManagerFactoryInfo
public String getPersistenceUnitName()
getPersistenceUnitName
in interface org.springframework.orm.jpa.EntityManagerFactoryInfo
public DataSource getDataSource()
getDataSource
in interface org.springframework.orm.jpa.EntityManagerFactoryInfo
public org.springframework.orm.jpa.JpaDialect getJpaDialect()
getJpaDialect
in interface org.springframework.orm.jpa.EntityManagerFactoryInfo
public javax.persistence.spi.PersistenceProvider getPersistenceProvider()
getPersistenceProvider
in interface org.springframework.orm.jpa.EntityManagerFactoryInfo
public org.springframework.dao.DataAccessException translateExceptionIfPossible(RuntimeException ex)
translateExceptionIfPossible
in interface org.springframework.dao.support.PersistenceExceptionTranslator
public void setManagedClassNames(List<String> managedClassNames)
managedClassNames
- the List of managed class names to set on this factory beanpublic void setEntityManagerInterface(Class<? extends javax.persistence.EntityManager> emInterface)
The default will be taken from the specific JpaVendorAdapter, if any, or set to the standard
EntityManager
interface else.
emInterface
- the EntityManager
interface to useJpaVendorAdapter.getEntityManagerInterface()
,
EntityManagerFactoryInfo.getEntityManagerInterface()
public void setEntityManagerFactoryInterface(Class<? extends javax.persistence.EntityManagerFactory> emfInterface)
The default will be taken from the specific JpaVendorAdapter, if any, or set to the standard
EntityManagerFactory
interface else.
emfInterface
- the EntityManagerFactory
interface to useJpaVendorAdapter.getEntityManagerFactoryInterface()
public Map<String,Object> getJpaPropertyMap()
Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".
public void setJpaPropertyMap(Map<String,?> jpaProperties)
Persistence.createEntityManagerFactory
(if any).
Can be populated with a "map" or "props" element in XML bean definitions.
jpaProperties
- map of JPA properties to setPersistence.createEntityManagerFactory(String, java.util.Map)
,
PersistenceProvider.createContainerEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo, java.util.Map)
public void setJpaProperties(Properties jpaProperties)
Persistence.createEntityManagerFactory
(if any).
Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.
jpaProperties
- the properties to setPersistence.createEntityManagerFactory(String, java.util.Map)
,
PersistenceProvider.createContainerEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo, java.util.Map)
public void setPersistenceUnitName(String persistenceUnitName)
Default is none, indicating the default EntityManagerFactory configuration. The persistence provider will throw an exception if ambiguous EntityManager configurations are found.
persistenceUnitName
- the name of the persistence unitPersistence.createEntityManagerFactory(String)
public void setPackagesToScan(String... packagesToScan)
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
).
packagesToScan
- one or more base packages to search, analogous to Spring's component-scan configuration for
regular Spring componentsDefaultPersistenceUnitManager#setPackagesToScan(String...)}
public void setMappingResources(String... mappingResources)
<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
.
mappingResources
- one or more mapping resources to useDefaultPersistenceUnitManager#setMappingResources(String...)}
public void setDataSource(DataSource dataSource)
This is an alternative to keeping the JDBC configuration in persistence.xml
, passing in a Spring-managed
DataSource instead.
In JPA speak, a DataSource passed in here will be used as "nonJtaDataSource" on the PersistenceUnitInfo passed
to the PersistenceProvider, as well as overriding data source configuration in persistence.xml
(if any).
Note that this variant typically works for JTA transaction management as well; if it does not, consider using the
explicit setJtaDataSource(javax.sql.DataSource)
instead.
dataSource
- the DataSource to use for this EntityManagerFactoryPersistenceUnitInfo.getNonJtaDataSource()
,
DefaultPersistenceUnitManager#setDefaultDataSource(javax.sql.DataSource)}
public void setJtaDataSource(DataSource jtaDataSource)
This is an alternative to keeping the JDBC configuration in persistence.xml
, passing in a Spring-managed
DataSource instead.
In JPA speak, a DataSource passed in here will be used as "jtaDataSource" on the PersistenceUnitInfo passed to
the PersistenceProvider, as well as overriding data source configuration in persistence.xml
(if any).
jtaDataSource
- the JTA-enabled DataSource to use for this EntityManagerFactoryPersistenceUnitInfo.getJtaDataSource()
,
DefaultPersistenceUnitManager#setDefaultJtaDataSource(javax.sql.DataSource)}
public void setPersistenceProvider(javax.persistence.spi.PersistenceProvider persistenceProvider)
If not specified, the persistence provider will be taken from the JpaVendorAdapter (if any) or determined by the persistence unit deployment descriptor (as far as possible).
persistenceProvider
- the PersistenceProvider to setJpaVendorAdapter.getPersistenceProvider()
,
PersistenceProvider
,
Persistence
public void setJpaDialect(org.springframework.orm.jpa.JpaDialect jpaDialect)
This will be exposed through the EntityManagerFactoryInfo interface, to be picked up as default dialect by accessors that intend to use JpaDialect functionality.
jpaDialect
- the JPA dialect to setEntityManagerFactoryInfo.getJpaDialect()
public void setJpaVendorAdapter(org.springframework.orm.jpa.JpaVendorAdapter jpaVendorAdapter)
This will initialize appropriate defaults for the given provider, such as persistence provider class and JpaDialect, unless locally overridden in this FactoryBean.
jpaVendorAdapter
- the JpaVendorAdapter to setpublic void setPersistenceUnitPostProcessors(org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor... postProcessors)
Note that if executed before afterPropertiesSet()
then this factory bean may introduce its own post
processor instances. If invoked after, then this method will override internally configured post processors.
Such post-processors can, for example, register further entity classes and jar files, in addition to the metadata
read from persistence.xml
.
postProcessors
- one or more post processors to setCopyright © 2005–2016 The Kuali Foundation. All rights reserved.