Class EMFProviderSpring

java.lang.Object
com.isomorphic.jpa.EMFProviderSpring
All Implemented Interfaces:
EMFProviderInterface

public class EMFProviderSpring extends Object implements EMFProviderInterface
EMF provider for Spring framework.

Values from jpa.entityManagerFactory and jpa.transaction properties are used to locate EntityManagerFactory and JpaTransactionManager in spring application context. For this implementation to work these declarations should be in spring application context file:
To acquire spring ApplicationContext:

 <bean id="springApplicationContextProvider" class="com.isomorphic.spring.SpringApplicationContextProvider" />
 
To acquire EntityManagerFactory: <bean id="REFERENCE_TO_ENTITY_MANAGER_FACTORY" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> ... </bean> To acquire JpaTransactionManager: <bean id="REFERENCE_TO_TRANSACTION_MANAGER" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="REFERENCE_TO_ENTITY_MANAGER_FACTORY" /> </bean>
  • Field Details

    • emf

      protected jakarta.persistence.EntityManagerFactory emf
      Holds EntityManagerFactory.
    • txm

      protected org.springframework.orm.jpa.JpaTransactionManager txm
      Holds JpaTransactionManager.
  • Constructor Details

    • EMFProviderSpring

      public EMFProviderSpring()
      Creates new EMF provider. Values from jpa.entityManagerFactory and jpa.transaction properties are used to locate EntityManagerFactory and JpaTransactionManager in spring application context.
    • EMFProviderSpring

      public EMFProviderSpring(Properties config)
      Creates new EMF provider. Values from provided properties entityManagerFactory and transaction are used to locate EntityManagerFactory and JpaTransactionManager in spring application context.
      Parameters:
      config - Properties for provider configuration.
    • EMFProviderSpring

      public EMFProviderSpring(String entityManagerFactory, String transaction)
      Creates new EMF provider with specified context reference names.
      Parameters:
      entityManagerFactory - String name of context reference to entity manager factory.
      transaction - String name of context reference to transaction manager.
  • Method Details

    • get

      public jakarta.persistence.EntityManagerFactory get()
      Returns EntityManagerFactory.
      Specified by:
      get in interface EMFProviderInterface
      Returns:
      EntityManagerFactory acquired from spring application context.
    • getEntityManager

      public jakarta.persistence.EntityManager getEntityManager() throws jakarta.persistence.PersistenceException
      Returns EntityManager created using factory.
      Specified by:
      getEntityManager in interface EMFProviderInterface
      Returns:
      EntityManager for persistence operations.
      Throws:
      jakarta.persistence.PersistenceException - if unable to acquire EntityManager.
    • returnEntityManager

      public void returnEntityManager(jakarta.persistence.EntityManager em)
      Closes EntityManager.
      Specified by:
      returnEntityManager in interface EMFProviderInterface
      Parameters:
      em - EntityManager which should be closed.
    • getTransaction

      public Object getTransaction(jakarta.persistence.EntityManager em) throws Exception
      Returns started transaction.

      Acquires transaction via JpaTransactionManager.getTransaction (null).

      Specified by:
      getTransaction in interface EMFProviderInterface
      Parameters:
      em - EntityManager needed if joining to existing transaction or starting transaction locally.
      Returns:
      Object Already started transaction for persistence operations.
      Throws:
      jakarta.persistence.PersistenceException - if unable to acquire or open transaction.
      Exception - if unable to acquire or open transaction.
    • commitTransaction

      public void commitTransaction(Object tx) throws Exception
      Commits specified transaction.
      Specified by:
      commitTransaction in interface EMFProviderInterface
      Parameters:
      tx - Object transaction to be committed.
      Throws:
      Exception - occurred while committing transaction.
    • rollbackTransaction

      public void rollbackTransaction(Object tx)
      Rolls back specified transaction.
      Specified by:
      rollbackTransaction in interface EMFProviderInterface
      Parameters:
      tx - Object transaction to be rolled back.