com.isomorphic.jpa

## Class EMFProviderSpring

- com.isomorphic.jpa.EMFProviderSpring

- All Implemented Interfaces:
    EMFProviderInterface

      public class EMFProviderSpring
      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 Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected javax.persistence.EntityManagerFactory` | `emf` Holds `EntityManagerFactory`.
    `protected org.springframework.orm.jpa.JpaTransactionManager` | `txm` Holds `JpaTransactionManager`.

    - ### Fields inherited from interface com.isomorphic.jpa.EMFProviderInterface

      `ENTITY_MANAGER, ENTITY_MANAGER_FACTORY, JPA_ENTITY_MANAGER, JPA_ENTITY_MANAGER_FACTORY, JPA_PERSISTENCE_UNIT_NAME, JPA_TRANSACTION, PERSISTENCE_UNIT_NAME, TRANSACTION`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `EMFProviderSpring()` Creates new EMF provider.
    `EMFProviderSpring(java.util.Properties config)` Creates new EMF provider.
    `EMFProviderSpring(java.lang.String entityManagerFactory, java.lang.String transaction)` Creates new EMF provider with specified context reference names.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `commitTransaction(java.lang.Object tx)` Commits specified transaction.
    `javax.persistence.EntityManagerFactory` | `get()` Returns `EntityManagerFactory`.
    `javax.persistence.EntityManager` | `getEntityManager()` Returns `EntityManager` created using factory.
    `java.lang.Object` | `getTransaction(javax.persistence.EntityManager em)` Returns started transaction. Acquires transaction via `JpaTransactionManager.getTransaction (null)`.
    `void` | `returnEntityManager(javax.persistence.EntityManager em)` Closes `EntityManager`.
    `void` | `rollbackTransaction(java.lang.Object tx)` Rolls back specified transaction.

- - ### Field Detail

    - #### emf

          protected javax.persistence.EntityManagerFactory emf

      Holds `EntityManagerFactory`.

    - #### txm

          protected org.springframework.orm.jpa.JpaTransactionManager txm

      Holds `JpaTransactionManager`.

  - ### Constructor Detail

    - #### 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(java.util.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(java.lang.String entityManagerFactory,
                           java.lang.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 Detail

    - #### get

          public javax.persistence.EntityManagerFactory get()

      Returns `EntityManagerFactory`.
      Specified by:
        `get` in interface `EMFProviderInterface`
      Returns:
        `EntityManagerFactory` acquired from spring application context.

    - #### getEntityManager

          public javax.persistence.EntityManager getEntityManager()
                                                           throws javax.persistence.PersistenceException

      Returns `EntityManager` created using factory.
      Specified by:
        `getEntityManager` in interface `EMFProviderInterface`
      Returns:
        `EntityManager` for persistence operations.
      Throws:
        `javax.persistence.PersistenceException` - if unable to acquire `EntityManager`.

    - #### returnEntityManager

          public void returnEntityManager(javax.persistence.EntityManager em)

      Closes `EntityManager`.

      Specified by:
        `returnEntityManager` in interface `EMFProviderInterface`
      Parameters:
        `em` - `EntityManager` which should be closed.

    - #### getTransaction

          public java.lang.Object getTransaction(javax.persistence.EntityManager em)
                                          throws java.lang.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:
        `javax.persistence.PersistenceException` - if unable to acquire or open transaction.
        `java.lang.Exception` - if unable to acquire or open transaction.

    - #### commitTransaction

          public void commitTransaction(java.lang.Object tx)
                                 throws java.lang.Exception

      Commits specified transaction.
      Specified by:
        `commitTransaction` in interface `EMFProviderInterface`
      Parameters:
        `tx` - `Object` transaction to be committed.
      Throws:
        `java.lang.Exception` - occurred while committing transaction.

    - #### rollbackTransaction

          public void rollbackTransaction(java.lang.Object tx)

      Rolls back specified transaction.
      Specified by:
        `rollbackTransaction` in interface `EMFProviderInterface`
      Parameters:
        `tx` - `Object` transaction to be rolled back.