Package com.zfabrik.hibernate
Class EntityManagerUtil
java.lang.Object
com.zfabrik.hibernate.EntityManagerUtil
Utility functions for Entity Manager provisioning.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic javax.persistence.EntityManagerFactory
createEntityManagerFactory(String persistenceUnit)
Create an EMF using thePersistence
class running in a good naming environment.static javax.persistence.EntityManagerFactory
createEntityManagerFactory(String persistenceUnit, Map<?,?> properties)
Create an EMF using thePersistence
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.
-
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.
Create the EMF at initialization time like this:EntityManager em = EntityManagerUtil.getEntityManager( IComponentsLookup.INSTANCE.lookup( "com.zfabrik.jta/userTransaction", TransactionManager.class ), this.emf );
assuming you have athis.emf = ThreadUtil.cleanContextExecute( this.getClass().getClassLoader(), new Callable<EntityManagerFactory>() { @Override public EntityManagerFactory call() throws Exception { return EntityManagerUtil.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); } } );
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 useemf
- 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 thePersistence
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 toPersistence.createEntityManagerFactory(String, Map)
calls, except of the naming context -
createEntityManagerFactory
public static javax.persistence.EntityManagerFactory createEntityManagerFactory(String persistenceUnit)Create an EMF using thePersistence
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 toPersistence.createEntityManagerFactory(String, Map)
calls, except of the naming context
-