spring-orm

org.springframework.orm.jpa
Class AbstractEntityManagerFactoryBean

java.lang.Object
  extended by org.springframework.orm.jpa.AbstractEntityManagerFactoryBean
All Implemented Interfaces:
java.io.Serializable, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.FactoryBean<javax.persistence.EntityManagerFactory>, org.springframework.beans.factory.InitializingBean, org.springframework.dao.support.PersistenceExceptionTranslator, EntityManagerFactoryInfo
Direct Known Subclasses:
LocalContainerEntityManagerFactoryBean, LocalEntityManagerFactoryBean

public abstract class AbstractEntityManagerFactoryBean
extends java.lang.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, EntityManagerFactoryInfo, org.springframework.dao.support.PersistenceExceptionTranslator, java.io.Serializable

Abstract FactoryBean that creates a local JPA EntityManagerFactory instance within a Spring application context.

Encapsulates the common functionality between the different JPA bootstrap contracts (standalone as well as container).

Implements support for standard JPA configuration as well as Spring's JpaVendorAdapter abstraction, and controls the EntityManagerFactory's lifecycle.

This class also implements the PersistenceExceptionTranslator interface, as autodetected by Spring's org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor, for AOP-based translation of native exceptions to Spring DataAccessExceptions. Hence, the presence of e.g. LocalEntityManagerFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate JPA exceptions.

Since:
2.0
Author:
Juergen Hoeller, Rod Johnson
See Also:
LocalEntityManagerFactoryBean, LocalContainerEntityManagerFactoryBean, Serialized Form

Field Summary
protected  org.apache.commons.logging.Log logger
          Logger available to subclasses
 javax.persistence.EntityManagerFactory nativeEntityManagerFactory
          Raw EntityManagerFactory as returned by the PersistenceProvider
 
Constructor Summary
AbstractEntityManagerFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  javax.persistence.EntityManagerFactory createEntityManagerFactoryProxy(javax.persistence.EntityManagerFactory emf)
          Create a proxy of the given EntityManagerFactory.
protected abstract  javax.persistence.EntityManagerFactory createNativeEntityManagerFactory()
          Subclasses must implement this method to create the EntityManagerFactory that will be returned by the getObject() method.
 void destroy()
          Close the EntityManagerFactory on bean factory shutdown.
 java.lang.ClassLoader getBeanClassLoader()
          Return the ClassLoader that the application's beans are loaded with.
 javax.sql.DataSource getDataSource()
          Return the JDBC DataSource that this EntityManagerFactory obtains its JDBC Connections from.
 java.lang.Class<? extends javax.persistence.EntityManager> getEntityManagerInterface()
          Return the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers will implement.
 JpaDialect getJpaDialect()
          Return the vendor-specific JpaDialect implementation for this EntityManagerFactory, or null if not known.
 java.util.Map<java.lang.String,java.lang.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.
 JpaVendorAdapter getJpaVendorAdapter()
          Return the JpaVendorAdapter implementation for this EntityManagerFactory, or null if not known.
 javax.persistence.EntityManagerFactory getNativeEntityManagerFactory()
          Return the raw underlying EntityManagerFactory.
 javax.persistence.EntityManagerFactory getObject()
          Return the singleton EntityManagerFactory.
 java.lang.Class<? extends javax.persistence.EntityManagerFactory> getObjectType()
           
 javax.persistence.spi.PersistenceProvider getPersistenceProvider()
          Return the underlying PersistenceProvider that the underlying EntityManagerFactory was created with.
 javax.persistence.spi.PersistenceUnitInfo getPersistenceUnitInfo()
          Return the PersistenceUnitInfo used to create this EntityManagerFactory, if the in-container API was used.
 java.lang.String getPersistenceUnitName()
          Return the name of the persistence unit used to create this EntityManagerFactory, or null if it is an unnamed default.
 boolean isSingleton()
           
 void setBeanClassLoader(java.lang.ClassLoader classLoader)
           
 void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
           
 void setBeanName(java.lang.String name)
           
 void setEntityManagerFactoryInterface(java.lang.Class<? extends javax.persistence.EntityManagerFactory> emfInterface)
          Specify the (potentially vendor-specific) EntityManagerFactory interface that this EntityManagerFactory proxy is supposed to implement.
 void setEntityManagerInterface(java.lang.Class<? extends javax.persistence.EntityManager> emInterface)
          Specify the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers are supposed to implement.
 void setJpaDialect(JpaDialect jpaDialect)
          Specify the vendor-specific JpaDialect implementation to associate with this EntityManagerFactory.
 void setJpaProperties(java.util.Properties jpaProperties)
          Specify JPA properties, to be passed into Persistence.createEntityManagerFactory (if any).
 void setJpaPropertyMap(java.util.Map<java.lang.String,?> jpaProperties)
          Specify JPA properties as a Map, to be passed into Persistence.createEntityManagerFactory (if any).
 void setJpaVendorAdapter(JpaVendorAdapter jpaVendorAdapter)
          Specify the JpaVendorAdapter implementation for the desired JPA provider, if any.
 void setPersistenceProvider(javax.persistence.spi.PersistenceProvider persistenceProvider)
          Set the PersistenceProvider instance to use for creating the EntityManagerFactory.
 void setPersistenceProviderClass(java.lang.Class<? extends javax.persistence.spi.PersistenceProvider> persistenceProviderClass)
          Set the PersistenceProvider implementation class to use for creating the EntityManagerFactory.
 void setPersistenceUnitName(java.lang.String persistenceUnitName)
          Specify the name of the EntityManagerFactory configuration.
 org.springframework.dao.DataAccessException translateExceptionIfPossible(java.lang.RuntimeException ex)
          Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
protected  java.lang.Object writeReplace()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Logger available to subclasses


nativeEntityManagerFactory

public javax.persistence.EntityManagerFactory nativeEntityManagerFactory
Raw EntityManagerFactory as returned by the PersistenceProvider

Constructor Detail

AbstractEntityManagerFactoryBean

public AbstractEntityManagerFactoryBean()
Method Detail

setPersistenceProviderClass

public void setPersistenceProviderClass(java.lang.Class<? extends javax.persistence.spi.PersistenceProvider> persistenceProviderClass)
Set the PersistenceProvider implementation class to use for creating the EntityManagerFactory. If not specified, the persistence provider will be taken from the JpaVendorAdapter (if any) or retrieved through scanning (as far as possible).

See Also:
JpaVendorAdapter.getPersistenceProvider(), PersistenceProvider, Persistence

setPersistenceProvider

public void setPersistenceProvider(javax.persistence.spi.PersistenceProvider persistenceProvider)
Set the PersistenceProvider instance to use for creating the EntityManagerFactory. 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).

See Also:
JpaVendorAdapter.getPersistenceProvider(), PersistenceProvider, Persistence

getPersistenceProvider

public javax.persistence.spi.PersistenceProvider getPersistenceProvider()
Description copied from interface: EntityManagerFactoryInfo
Return the underlying PersistenceProvider that the underlying EntityManagerFactory was created with.

Specified by:
getPersistenceProvider in interface EntityManagerFactoryInfo
Returns:
the PersistenceProvider used to create this EntityManagerFactory, or null if the standard JPA provider autodetection process was used to configure the EntityManagerFactory

setPersistenceUnitName

public void setPersistenceUnitName(java.lang.String persistenceUnitName)
Specify the name of the EntityManagerFactory configuration.

Default is none, indicating the default EntityManagerFactory configuration. The persistence provider will throw an exception if ambiguous EntityManager configurations are found.

See Also:
Persistence.createEntityManagerFactory(String)

getPersistenceUnitName

public java.lang.String getPersistenceUnitName()
Description copied from interface: EntityManagerFactoryInfo
Return the name of the persistence unit used to create this EntityManagerFactory, or null if it is an unnamed default.

If getPersistenceUnitInfo() returns non-null, the result of getPersistenceUnitName() must be equal to the value returned by PersistenceUnitInfo.getPersistenceUnitName().

Specified by:
getPersistenceUnitName in interface EntityManagerFactoryInfo
See Also:
EntityManagerFactoryInfo.getPersistenceUnitInfo(), PersistenceUnitInfo.getPersistenceUnitName()

setJpaProperties

public void setJpaProperties(java.util.Properties jpaProperties)
Specify JPA properties, to be passed into Persistence.createEntityManagerFactory (if any).

Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.

See Also:
Persistence.createEntityManagerFactory(String, java.util.Map), PersistenceProvider.createContainerEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo, java.util.Map)

setJpaPropertyMap

public void setJpaPropertyMap(java.util.Map<java.lang.String,?> jpaProperties)
Specify JPA properties as a Map, to be passed into Persistence.createEntityManagerFactory (if any).

Can be populated with a "map" or "props" element in XML bean definitions.

See Also:
Persistence.createEntityManagerFactory(String, java.util.Map), PersistenceProvider.createContainerEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo, java.util.Map)

getJpaPropertyMap

public java.util.Map<java.lang.String,java.lang.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.

Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".


setEntityManagerFactoryInterface

public void setEntityManagerFactoryInterface(java.lang.Class<? extends javax.persistence.EntityManagerFactory> emfInterface)
Specify the (potentially vendor-specific) EntityManagerFactory interface that this EntityManagerFactory proxy is supposed to implement.

The default will be taken from the specific JpaVendorAdapter, if any, or set to the standard javax.persistence.EntityManagerFactory interface else.

See Also:
JpaVendorAdapter.getEntityManagerFactoryInterface()

setEntityManagerInterface

public void setEntityManagerInterface(java.lang.Class<? extends javax.persistence.EntityManager> emInterface)
Specify the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers are supposed to implement.

The default will be taken from the specific JpaVendorAdapter, if any, or set to the standard javax.persistence.EntityManager interface else.

See Also:
JpaVendorAdapter.getEntityManagerInterface(), EntityManagerFactoryInfo.getEntityManagerInterface()

getEntityManagerInterface

public java.lang.Class<? extends javax.persistence.EntityManager> getEntityManagerInterface()
Description copied from interface: EntityManagerFactoryInfo
Return the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers will implement.

A null return value suggests that autodetection is supposed to happen: either based on a target EntityManager instance or simply defaulting to javax.persistence.EntityManager.

Specified by:
getEntityManagerInterface in interface EntityManagerFactoryInfo

setJpaDialect

public void setJpaDialect(JpaDialect jpaDialect)
Specify the vendor-specific JpaDialect implementation to associate with this EntityManagerFactory. This will be exposed through the EntityManagerFactoryInfo interface, to be picked up as default dialect by accessors that intend to use JpaDialect functionality.

See Also:
EntityManagerFactoryInfo.getJpaDialect()

getJpaDialect

public JpaDialect getJpaDialect()
Description copied from interface: EntityManagerFactoryInfo
Return the vendor-specific JpaDialect implementation for this EntityManagerFactory, or null if not known.

Specified by:
getJpaDialect in interface EntityManagerFactoryInfo

setJpaVendorAdapter

public void setJpaVendorAdapter(JpaVendorAdapter jpaVendorAdapter)
Specify the JpaVendorAdapter implementation for the desired JPA provider, if any. This will initialize appropriate defaults for the given provider, such as persistence provider class and JpaDialect, unless locally overridden in this FactoryBean.


getJpaVendorAdapter

public JpaVendorAdapter getJpaVendorAdapter()
Return the JpaVendorAdapter implementation for this EntityManagerFactory, or null if not known.


setBeanClassLoader

public void setBeanClassLoader(java.lang.ClassLoader classLoader)
Specified by:
setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAware

getBeanClassLoader

public java.lang.ClassLoader getBeanClassLoader()
Description copied from interface: EntityManagerFactoryInfo
Return the ClassLoader that the application's beans are loaded with.

Proxies will be generated in this ClassLoader.

Specified by:
getBeanClassLoader in interface EntityManagerFactoryInfo

setBeanFactory

public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
Specified by:
setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware

setBeanName

public void setBeanName(java.lang.String name)
Specified by:
setBeanName in interface org.springframework.beans.factory.BeanNameAware

afterPropertiesSet

public final void afterPropertiesSet()
                              throws javax.persistence.PersistenceException
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
javax.persistence.PersistenceException

createEntityManagerFactoryProxy

protected javax.persistence.EntityManagerFactory createEntityManagerFactoryProxy(javax.persistence.EntityManagerFactory emf)
Create a proxy of the given EntityManagerFactory. We do this to be able to return transaction-aware proxies for application-managed EntityManagers, and to introduce the NamedEntityManagerFactory interface

Parameters:
emf - EntityManagerFactory as returned by the persistence provider
Returns:
proxy entity manager

createNativeEntityManagerFactory

protected abstract javax.persistence.EntityManagerFactory createNativeEntityManagerFactory()
                                                                                    throws javax.persistence.PersistenceException
Subclasses must implement this method to create the EntityManagerFactory that will be returned by the getObject() method.

Returns:
EntityManagerFactory instance returned by this FactoryBean
Throws:
javax.persistence.PersistenceException - if the EntityManager cannot be created

translateExceptionIfPossible

public org.springframework.dao.DataAccessException translateExceptionIfPossible(java.lang.RuntimeException ex)
Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.

Uses the dialect's conversion if possible; otherwise falls back to standard JPA exception conversion.

Specified by:
translateExceptionIfPossible in interface org.springframework.dao.support.PersistenceExceptionTranslator
See Also:
org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor, PersistenceExceptionTranslator.translateExceptionIfPossible(java.lang.RuntimeException), EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(java.lang.RuntimeException)

getNativeEntityManagerFactory

public javax.persistence.EntityManagerFactory getNativeEntityManagerFactory()
Description copied from interface: EntityManagerFactoryInfo
Return the raw underlying EntityManagerFactory.

Specified by:
getNativeEntityManagerFactory in interface EntityManagerFactoryInfo
Returns:
the unadorned EntityManagerFactory (never null)

getPersistenceUnitInfo

public javax.persistence.spi.PersistenceUnitInfo getPersistenceUnitInfo()
Description copied from interface: EntityManagerFactoryInfo
Return the PersistenceUnitInfo used to create this EntityManagerFactory, if the in-container API was used.

Specified by:
getPersistenceUnitInfo in interface EntityManagerFactoryInfo
Returns:
the PersistenceUnitInfo used to create this EntityManagerFactory, or null if the in-container contract was not used to configure the EntityManagerFactory

getDataSource

public javax.sql.DataSource getDataSource()
Description copied from interface: EntityManagerFactoryInfo
Return the JDBC DataSource that this EntityManagerFactory obtains its JDBC Connections from.

Specified by:
getDataSource in interface EntityManagerFactoryInfo
Returns:
the JDBC DataSource, or null if not known

getObject

public javax.persistence.EntityManagerFactory getObject()
Return the singleton EntityManagerFactory.

Specified by:
getObject in interface org.springframework.beans.factory.FactoryBean<javax.persistence.EntityManagerFactory>

getObjectType

public java.lang.Class<? extends javax.persistence.EntityManagerFactory> getObjectType()
Specified by:
getObjectType in interface org.springframework.beans.factory.FactoryBean<javax.persistence.EntityManagerFactory>

isSingleton

public boolean isSingleton()
Specified by:
isSingleton in interface org.springframework.beans.factory.FactoryBean<javax.persistence.EntityManagerFactory>

destroy

public void destroy()
Close the EntityManagerFactory on bean factory shutdown.

Specified by:
destroy in interface org.springframework.beans.factory.DisposableBean

writeReplace

protected java.lang.Object writeReplace()
                                 throws java.io.ObjectStreamException
Throws:
java.io.ObjectStreamException

spring-orm