spring-framework / org.springframework.orm.jpa.support / SharedEntityManagerBean

SharedEntityManagerBean

open class SharedEntityManagerBean : EntityManagerFactoryAccessor, FactoryBean<EntityManager>, InitializingBean

FactoryBean that exposes a shared JPA javax.persistence.EntityManager reference for a given EntityManagerFactory. Typically used for an EntityManagerFactory created by org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean, as direct alternative to a JNDI lookup for a Java EE server's EntityManager reference.

The shared EntityManager will behave just like an EntityManager fetched from an application server's JNDI environment, as defined by the JPA specification. It will delegate all calls to the current transactional EntityManager, if any; otherwise, it will fall back to a newly created EntityManager per operation.

Can be passed to DAOs that expect a shared EntityManager reference rather than an EntityManagerFactory. Note that Spring's org.springframework.orm.jpa.JpaTransactionManager always needs an EntityManagerFactory in order to create new transactional EntityManager instances.

Author
Juergen Hoeller

Since
2.0

See Also
#setEntityManagerFactory#setEntityManagerInterfaceorg.springframework.orm.jpa.LocalEntityManagerFactoryBeanorg.springframework.orm.jpa.JpaTransactionManager

Constructors

<init>

SharedEntityManagerBean()

FactoryBean that exposes a shared JPA javax.persistence.EntityManager reference for a given EntityManagerFactory. Typically used for an EntityManagerFactory created by org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean, as direct alternative to a JNDI lookup for a Java EE server's EntityManager reference.

The shared EntityManager will behave just like an EntityManager fetched from an application server's JNDI environment, as defined by the JPA specification. It will delegate all calls to the current transactional EntityManager, if any; otherwise, it will fall back to a newly created EntityManager per operation.

Can be passed to DAOs that expect a shared EntityManager reference rather than an EntityManagerFactory. Note that Spring's org.springframework.orm.jpa.JpaTransactionManager always needs an EntityManagerFactory in order to create new transactional EntityManager instances.

Functions

afterPropertiesSet

fun afterPropertiesSet(): Unit

getObject

open fun getObject(): EntityManager

getObjectType

open fun getObjectType(): Class<out EntityManager>

isSingleton

open fun isSingleton(): Boolean

setEntityManagerInterface

open fun setEntityManagerInterface(entityManagerInterface: Class<out EntityManager>): Unit

Specify the EntityManager interface to expose.

Default is the EntityManager interface as defined by the EntityManagerFactoryInfo, if available. Else, the standard javax.persistence.EntityManager interface will be used.

setSynchronizedWithTransaction

open fun setSynchronizedWithTransaction(synchronizedWithTransaction: Boolean): Unit

Set whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules). Default is "true".

Inherited Functions

getEntityManagerFactory

open fun getEntityManagerFactory(): EntityManagerFactory

Return the JPA EntityManagerFactory that should be used to create EntityManagers.

getJpaPropertyMap

open fun getJpaPropertyMap(): MutableMap<String, Any>

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]".

getPersistenceUnitName

open fun getPersistenceUnitName(): String

Return the name of the persistence unit to access the EntityManagerFactory for, if any.

setBeanFactory

open fun setBeanFactory(beanFactory: BeanFactory): Unit

Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly. Falls back to a default EntityManagerFactory bean if no persistence unit specified.

setEntityManagerFactory

open fun setEntityManagerFactory(emf: EntityManagerFactory): Unit

Set the JPA EntityManagerFactory that should be used to create EntityManagers.

setJpaProperties

open fun setJpaProperties(jpaProperties: Properties): Unit

Specify JPA properties, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

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

setJpaPropertyMap

open fun setJpaPropertyMap(jpaProperties: MutableMap<String, Any>): Unit

Specify JPA properties as a Map, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

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

setPersistenceUnitName

open fun setPersistenceUnitName(persistenceUnitName: String): Unit

Set the name of the persistence unit to access the EntityManagerFactory for.

This is an alternative to specifying the EntityManagerFactory by direct reference, resolving it by its persistence unit name instead. If no EntityManagerFactory and no persistence unit name have been specified, a default EntityManagerFactory will be retrieved through finding a single unique bean of type EntityManagerFactory.