Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* JpaUtil.java
  
   {{IS_NOTE
   Purpose:
   
   Description:
   
   History:
   Mon Dec 17 2007, Created by jeffliu
  }}IS_NOTE
 
  Copyright (C) 2006 Potix Corporation. All Rights Reserved.
 
  {{IS_RIGHT
  }}IS_RIGHT
  */
 
 package org.zkoss.zkplus.jpa;
 
 import java.util.Map;
 
 import  javax.persistence.EntityManager;
 import  javax.persistence.EntityManagerFactory;
 import  javax.persistence.Persistence;
 
 import  org.hibernate.HibernateException;
This class is used to create and hold open EntityManagerFactory objects within a Java EE environment.

Author(s):
Jeff
Since:
3.0.2
 
 public class JpaUtil {
 
 	private static final Log log = Log.lookup(JpaUtil.class);
 
 	public static final String CONFIG = "JpaUtil.PersistenceUnitName";
 
 	public static final String JPA_EMF_MAP = "org.zkoss.zkplus.jpa.EmfMap";
 
 	public static final String JPA_EM_MAP = "org.zkoss.zkplus.jpa.EmMap";
 
 	/*
 	 * Get the EntityManagerFactories Map from WebApp scope
 	 */
 	private static Map getEmfMap() {
 		final Map appattrs = getWebApp().getAttributes(); 
 		Map map = (Mapappattrs.get();
 		if (map == null) {
 			map = new HashMap();
 			appattrs.put(map);
 		}
 		return map;
 	}
 
 	/*
 	 * Get the EntityManagers Map from Execution scope
 	 */
 	private static Map getEmMap() {
 		Execution current = Executions.getCurrent();
 		Map map = (Mapcurrent.getAttribute();
 		if (map == null) {
 			map = new HashMap();
 			Executions.getCurrent().setAttribute(map);
 		}
 		return map;
 	}

Create or return the default EntityManagerFactory as defined in zk.xml.

In WEB-INF/zk.xml, add following lines:


<preference>
 <name>JPA.PersistenceUnitName</name>
 <value>PERSISTENCE_UNIT_NAME</value>
</preference>
 

Returns:
EntityManagerFactory
 
 	public static EntityManagerFactory getEntityManagerFactory() {
 		return initEntityManagerFactory(nullnull);
 	}

Create or return the EntityManagerFactory for the specified persistence unit name. *Notice:If the EntityManagerFactory with specified persistence unit is not created yet, a new one will be created.

Parameters:
puName Persistence unit name
Returns:
EntityManagerFactory
	public static EntityManagerFactory getEntityManagerFactory(String puName) {
		return initEntityManagerFactory(puNamenull);
	}

Create the EntityManagerFactory for the specified persistence unit and defined properties. *Notice: It always creates a new EntityManagerFactory unless properties is null.

Parameters:
puName Persistence unit name
properties Defined priorities
Returns:
EntityManagerFactory
	public static EntityManagerFactory getEntityManagerFactory(String puName,
	Map properties) {
		return initEntityManagerFactory(puNameproperties);
	}

Returns an EntityManager of the default EntityManagerFactory as defined in zk.xml in an Execution scope.

In WEB-INF/zk.xml, add following lines:


<preference>
 <name>JPA.PersistenceUnitName</name>
 <value>PERSISTENCE_UNIT_NAME</value>
</preference>
 

Returns:
EntityManager
	public static EntityManager getEntityManager() {
		return initEntityManger(nullnull);
	}

Returns or create an EntityManager for the specified persistence unit name. *Notice:If the EntityManagerFactory with specified presistence unit is not created before, a new one will be created automatically.

The EntityManager get by this method is guaranteed to be the same within one Execution for the specified persistence unit name.

Parameters:
puName - Persistence unit name
Returns:
EntityManager
	public static EntityManager getEntityManager(String puName) {
		return initEntityManger(puNamenull);
	}

Closes the EntityManager of the default EntityManagerFactory as defined in zk.xml in an Execution scope. It is equals to closeEntityManager(null).

In WEB-INF/zk.xml, add following lines:


<preference>
 <name>JPA.PersistenceUnitName</name>
 <value>PERSISTENCE_UNIT_NAME</value>
</preference>
 

Since:
3.0.7
	public static void closeEntityManager() {
	}

Closes the EntityManager of the EntityManagerFactory of the specified puName;

Since:
3.0.7
	public static void closeEntityManager(String puName) {
		EntityManager em = (EntityManager) getEmMap().remove(getPersistenceUnitName(puName));
		if (em != null && em.isOpen()) em.close();
	}

Returns an EntityManager for the specified persistence unit name and defined properties. *Notice: It always creates a new EntityManagerFactory and thus a new EntityManager unless properties is null.

Parameters:
puName - Persistence unit name
properties - Defined priorities
Returns:
EntityManager
	public static EntityManager getEntiyManager(String puNameMap properties) {
		return initEntityManger(puNameproperties);
	}
	/*
	 * If EntityManagerFactory with persistence name puName is not found in Map,
	 * created a new one and return; or return a new created EntityManagerFactory
	 * directly.
	 */
	private static EntityManagerFactory initEntityManagerFactory(String puNameMap properties) {
		EntityManagerFactory emf;
		if (properties == null) {
			emf = (EntityManagerFactory) getEmfMap().get(puName);
			if (emf == null) {
				emf = createEntityManagerFactory(puNamenull);
				getEmfMap().put(puNameemf);
			}
else {
			emf = createEntityManagerFactory(puNameproperties);
			getEmfMap().put(puNameemf);
		}
		return emf;
	}
	/*
	 * If EntityManager with persistence name puName is not found in Map,
	 * created a new one; or return the existing EntityManager of this execution directly.
	 */
	private static EntityManager initEntityManger(String puName,Map properties) {
		EntityManager em;
		if (properties == null) {
			em = (EntityManager) getEmMap().get(puName);
			if (em == null) {
				em = createEntityManager(puNamenull);
				getEmMap().put(puNameem);
			}
else {
			em = createEntityManager(puNameproperties);
			getEmMap().put(puNameem);
		}
		return em;
	}
	/*
	 * Create the EntityManagerFactory by persistence unit name. If persistence
	 * unit name not given, using the one which is defined in zk.xml
	 */
	private static EntityManagerFactory createEntityManagerFactory(
	String puNameMap properties) {
		puName = getPersistenceUnitName(puName);
		EntityManagerFactory emf;
		try {
			if (properties == null) {
				emf = Persistence.createEntityManagerFactory(puName);
				.info("EntityManagerFactory for: " + puName + " is created ");
else {
				emf = Persistence.createEntityManagerFactory(puNameproperties);
				.info("EntityManagerFactory for: " + puName + " with properties "+properties + " is created ");
			}
catch (Exception ex) {
			.error("Initial EntityManagerFactory creation failed." + ex);
		}
		getEmfMap().put(puNameemf);
		return emf;
	}
	/*
	 * Create the EntityManager by persistence unit name. If persistence unit
	 * name not given, using the one which is defined in zk.xml
	 */
	private static EntityManager createEntityManager(String puNameMap properties) {
		puName = getPersistenceUnitName(puName);
		EntityManager em = initEntityManagerFactory(puNameproperties).createEntityManager();
		return em;
	}
	private static WebApp getWebApp() {
		WebApp app = null;
		final Execution exec = Executions.getCurrent();
		if (exec != null) {
			final Desktop desktop = exec.getDesktop();
			if (desktop != null) {
				app = desktop.getWebApp();
			}
		}
		return app;
	}
	private static String getPersistenceUnitName(String pu) {
		if (pu == null || pu.equals("")) {
			/*
			 * Create EntityManagerFactory by preference in zk.xml
			 */
			pu = config.getPreference(null);
		}
		if (pu == null) {
			throw new UiException("Forget to specify the preference of "++" in WEB-INF/zk.xml?");
		}
		return pu;
	}
New to GrepCode? Check out our FAQ X