Class EntityManagerUtil

java.lang.Object
com.zfabrik.hibernate.EntityManagerUtil

public class EntityManagerUtil extends Object
Utility functions for Entity Manager provisioning.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static javax.persistence.EntityManagerFactory
    createEntityManagerFactory​(String persistenceUnit)
    Create an EMF using the Persistence class running in a good naming environment.
    static javax.persistence.EntityManagerFactory
    createEntityManagerFactory​(String persistenceUnit, Map<?,​?> properties)
    Create an EMF using the Persistence class running in a good naming environment.
    static javax.persistence.EntityManager
    getEntityManager​(javax.transaction.TransactionManager tm, javax.persistence.EntityManagerFactory emf)
    Get an entity manager given a transaction manager and an entity manager factory.
    static void
    releaseEntityManager​(javax.persistence.EntityManagerFactory emf)
    Release an entity manager factory from the current TX.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • EntityManagerUtil

      public EntityManagerUtil()
  • Method Details

    • getEntityManager

      public static javax.persistence.EntityManager getEntityManager(javax.transaction.TransactionManager tm, javax.persistence.EntityManagerFactory emf)
      Get an entity manager given a transaction manager and an entity manager factory. Note, given an entity manager factory always pass the same transaction manager. The transaction manager is used to register a synchronization so that the following behavior can be implemented:
      • For every invocation within the same transaction, return the same entity manager.
      • Disassociate the entity manager from the transaction, when it completed.
      In conjunction with Z2's built-in JTA implementation, use this method like this whenever you need an EM:
      
       EntityManager em = EntityManagerUtil.getEntityManager(
         IComponentsLookup.INSTANCE.lookup(
           "com.zfabrik.jta/userTransaction",
           TransactionManager.class
         ),
         this.emf
       );
       
      Create the EMF at initialization time like this:
      
       this.emf = ThreadUtil.cleanContextExecute(
         this.getClass().getClassLoader(),
         new Callable<EntityManagerFactory>() {
           @Override
           public EntityManagerFactory call() throws Exception {
             return EntityManagerUtil.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
           }
         }
       );
       
      assuming you have a META-INF/persistence.xml in your implementation that is in the same class loader scope as the code above. See the example Sample-hibernate-basic for more details.
      Parameters:
      tm - The transaction manager in use
      emf - The entity manager factory to use when a new entity manager needs to be created
      Returns:
    • releaseEntityManager

      public static void releaseEntityManager(javax.persistence.EntityManagerFactory emf)
      Release an entity manager factory from the current TX. This is happening automatically at the end of a TX.
    • createEntityManagerFactory

      public static javax.persistence.EntityManagerFactory createEntityManagerFactory(String persistenceUnit, Map<?,​?> properties)
      Create an EMF using the Persistence class running in a good naming environment. Hibernate JNDI lookup is broken in multiple ways (see e.g. https://hibernate.atlassian.net/browse/HHH-8818) One way in which it is broken is that it asks for a name parser before a URL lookup which fails if there is no ICF on the jndi.properties defaults (or elsewhere) - which is nonsensical in the first place. So effectively, calling this method is equivalent to Persistence.createEntityManagerFactory(String, Map) calls, except of the naming context
    • createEntityManagerFactory

      public static javax.persistence.EntityManagerFactory createEntityManagerFactory(String persistenceUnit)
      Create an EMF using the Persistence class running in a good naming environment. Hibernate JNDI lookup is broken in multiple ways (see e.g. https://hibernate.atlassian.net/browse/HHH-8818) One way in which it is broken is that it asks for a name parser before a URL lookup which fails if there is no ICF on the jndi.properties defaults (or elsewhere) - which is nonsensical in the first place. So effectively, calling this method is equivalent to Persistence.createEntityManagerFactory(String, Map) calls, except of the naming context