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:
  • 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:
    • 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>