Package org.springframework.orm.jpa.support

# Class SharedEntityManagerBean

java.lang.Object
org.springframework.orm.jpa.EntityManagerFactoryAccessor
org.springframework.orm.jpa.support.SharedEntityManagerBean

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanFactoryAware`, `org.springframework.beans.factory.FactoryBean<EntityManager>`, `org.springframework.beans.factory.InitializingBean`

public class SharedEntityManagerBean extends EntityManagerFactoryAccessor implements org.springframework.beans.factory.FactoryBean<EntityManager>, org.springframework.beans.factory.InitializingBean
`FactoryBean` that exposes a shared JPA `EntityManager` reference for a given EntityManagerFactory. Typically used for an EntityManagerFactory created by `LocalContainerEntityManagerFactoryBean`, as direct alternative to a JNDI lookup for a Jakarta 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 `JpaTransactionManager` always needs an EntityManagerFactory in order to create new transactional EntityManager instances.

Since:
  2.0
Author:
  Juergen Hoeller
See Also:
  - `EntityManagerFactoryAccessor.setEntityManagerFactory(jakarta.persistence.EntityManagerFactory)`
  - `setEntityManagerInterface(java.lang.Class<? extends jakarta.persistence.EntityManager>)`
  - `LocalEntityManagerFactoryBean`
  - `JpaTransactionManager`

## Field Summary

### Fields inherited from class org.springframework.orm.jpa.EntityManagerFactoryAccessor

`logger`

### Fields inherited from interface org.springframework.beans.factory.FactoryBean

`OBJECT_TYPE_ATTRIBUTE`

## Constructor Summary

Constructors
Constructor
Description
`SharedEntityManagerBean()`

## Method Summary

Modifier and Type
Method
Description
`final void`
`afterPropertiesSet()`
`EntityManager`
`getObject()`
`Class<? extends EntityManager>`
`getObjectType()`
`boolean`
`isSingleton()`
`void`
`setEntityManagerInterface(Class<? extends EntityManager> entityManagerInterface)`
Specify the EntityManager interface to expose.
`void`
`setSynchronizedWithTransaction(boolean synchronizedWithTransaction)`
Set whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules).

### Methods inherited from class org.springframework.orm.jpa.EntityManagerFactoryAccessor

`createEntityManager, getEntityManagerFactory, getJpaPropertyMap, getPersistenceUnitName, getTransactionalEntityManager, obtainEntityManagerFactory, setBeanFactory, setEntityManagerFactory, setJpaProperties, setJpaPropertyMap, setPersistenceUnitName`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### SharedEntityManagerBean

public SharedEntityManagerBean()

## Method Details

### setEntityManagerInterface

public void setEntityManagerInterface(Class<? extends EntityManager> entityManagerInterface)
Specify the EntityManager interface to expose.

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

See Also:
  - `EntityManagerFactoryInfo.getEntityManagerInterface()`
  - `EntityManager`

### setSynchronizedWithTransaction

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

### afterPropertiesSet

public final void afterPropertiesSet()

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`

### getObject

@Nullable public EntityManager getObject()

Specified by:
  `getObject` in interface `org.springframework.beans.factory.FactoryBean<EntityManager>`

### getObjectType

public Class<? extends EntityManager> getObjectType()

Specified by:
  `getObjectType` in interface `org.springframework.beans.factory.FactoryBean<EntityManager>`

### isSingleton

public boolean isSingleton()

Specified by:
  `isSingleton` in interface `org.springframework.beans.factory.FactoryBean<EntityManager>`