From Documentation

Jump to: navigation, search




  • Author
    Jeff Liu, Engineer, Potix Corporation
  • Date
    November 29, 2007
  • Version
    Applicable to ZK 3.0.2 Freshly (zk-3.0.2-FL-2007-12-24 and later)
Applicable to JBoss AS 4.0.5.GA


Contents

Introduction

From pervious smalltalk Using ZK JndiVariableResolver - Retrieve Session Beans and EntityManagerFactory , developers should have a general idea about how to retrieve entityManagers and entityManagerFactories by JNDI binding. In this smalltalk, a JPA (Java Persistence API) specific method - retrieving an entityManager with JpaUtil is demonstrated. Also, OpenEntityManagerInView listener, adapting hibernate's "Open Session In View" pattern, is supported to use entityManager without taking care of transcation begin, commit, close.

Retrieving EntityManager in Zul Page

Following I am going to present you an example on how to use ZK JpaUtil to retrieve entity manager


JpaUtil

JpaUtil is an utility class for Java Persistence API, providing methods to retrieve entityManagers and entityManagerFactories. To retrieve entityManagers and entityManagerFactories, developers specify persistence unit name in zk.xml or pass persistence unit name as method parameter. Next, we are going to take a look at how to retrieve entityManager by persistence unit name.


Retrieve EntityManager by Persistence Unit Name

If developers want to use persistence unit name, can easily achieve by one line of code.


persistence.xml

<persistence>
	<persistence-unit name="DemoEJB3">
		<jta-data-source>java:/MySqlDS</jta-data-source>
		...
	</persistence-unit>
	<persistence-unit name="DemoEJB32">
		<jta-data-source>java:/DefaultDS</jta-data-source>
		...
	</persistence-unit>
</persistence>


person5.zul

<window>
	<zscript>
		import org.zkoss.zkplus.jpa.JpaUtil;
		import javax.persistence.EntityManager;
		import javax.persistence.EntityTransaction;
		import demo.Person;
		// Get entityManager from persistence unit which named "EJB32"
		EntityManager em = JpaUtil.getEntityManager("DemoEJB32");
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		Person p1 = new Person(null, "Robbie Chen", "rbc@demo.com");
		em.persist(p1);
		Person p2 = new Person(null, "Kevin Lee", "kl@demo.com");
		em.persist(p2);
		Person p3 = new Person(null, "John Korapav", "jkor@demo.com");
		em.persist(p3);
		List persons= em.createQuery("from Person").getResultList();
		tx.commit();
	
		em.close();
	</zscript>
	<listbox width="600px">
	<listhead sizable="true">
				<listheader label="name" sort="auto"/>
				<listheader label="email" sort="auto"/>
	</listhead>
	<listitem forEach="">
		<listcell label=""/>
		<listcell label=""/>
	</listitem>
	</listbox>
</window>


Snapshot:

JndiVR.png

Specified the Default Persistence Unit Name

By specify the default persistence unit name, developers don't need to write the persistence name repeatly. First, in zk.xml, specify the default persistence unit name which is defined in persistence.xml.

Ex:


zk.xml

<zk>
	<preference>
		<name>JpaUtil.PersistenceUnitName</name>
		<value>DemoEJB3</value>//The persistence unit name which is defined in persistence.xml
	</preference>
	...


persistence.xml

<persistence>
	<persistence-unit name="DemoEJB3">
	...

Get EntityManager from Default Persistence Unit

Once the default persistence unit name is specified in zk.xml, developer can retrieve the entityManager in only one line of code without writing the persistence unit name.


person4.zul

<window>
	<zscript>
		import org.zkoss.zkplus.jpa.JpaUtil;
		import javax.persistence.EntityManager;
		import javax.persistence.EntityTransaction;
		import demo.Person;
		// Get entityManager from default persistence unit
		EntityManager em = JpaUtil.getEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		Person p1 = new Person(null, "Robbie Chen", "rbc@demo.com");
		em.persist(p1);
		Person p2 = new Person(null, "Kevin Lee", "kl@demo.com");
		em.persist(p2);
		Person p3 = new Person(null, "John Korapav", "jkor@demo.com");
		em.persist(p3);
		List persons= em.createQuery("from Person").getResultList();
		tx.commit();
	
		em.close();
	</zscript>
	<listbox width="600px">
	<listhead sizable="true">
				<listheader label="name" sort="auto"/>
				<listheader label="email" sort="auto"/>
	</listhead>
	<listitem forEach="">
		<listcell label=""/>
		<listcell label=""/>
	</listitem>
	</listbox>
</window>

Using OpenEntityManagerInView

By using ZK OpenEntityManagerInView listener, adapting hibernate's "Open Session In View" pattern, developers now are able to use entityManager without caring of transcation begin, commit, close.
Specified OpenEntityManagerInView in zk.xml


zk.xml

<zk>
	<preference>
		<name>JpaUtil.PersistenceUnitName</name>
		<value>DemoEJB3</value>
	</preference>
	<!-- Specify JPA "OpenEntityManagerInView" Listener -->
	<listener>
 		<description>JPA "OpenEntityManagerInView" Listener</description>
 		<listener-class>org.zkoss.zkplus.jpa.OpenEntityManagerInViewListener</listener-class>
 	</listener>
</zk>


person6.zul

<window>
	<zscript>
		import org.zkoss.zkplus.jpa.JpaUtil;
		import javax.persistence.EntityManager;
		import javax.persistence.EntityTransaction;
		import demo.Person;
		// Get entityManager from default persistence unit without caring transcation
		EntityManager em = JpaUtil.getEntityManager();
	
		Person p1 = new Person(null, "Robbie Chen", "rbc@demo.com");
		em.persist(p1);
		Person p2 = new Person(null, "Kevin Lee", "kl@demo.com");
		em.persist(p2);
		Person p3 = new Person(null, "John Korapav", "jkor@demo.com");
		em.persist(p3);
		List persons= em.createQuery("from Person").getResultList();
		
	</zscript>
	<listbox width="600px">
	<listhead sizable="true">
				<listheader label="name" sort="auto"/>
				<listheader label="email" sort="auto"/>
	</listhead>
	<listitem forEach="">
		<listcell label=""/>
		<listcell label=""/>
	</listitem>
	</listbox>
</window>


Bundle ZK into JBoss by Eclipse

Step by Step

  • Create a Dynamic Web Project and bundle ZK to it, refer to ZK Quick Start Guide
  • Create an EJB3 Project
  • Create a JavaEE- Enterprise Application Project and setup the module dependencies for the dynamic web project and EJB3 project in previous steps
After the steps above, the Deployment Descriptor(application.xml) should be look like this:
<?xml version="1.0" encoding="UTF-8"?>
<application id="Application_ID" version="1.4" ...>
	<display-name>
	ZkEJB3Demo</display-name>
	<module>
		<web>
			<web-uri>DemoWeb.war</web-uri>
			<context-root>DemoWeb</context-root>
		</web>
	</module>
	<module >
		<ejb>DemoEJB3.jar</ejb>
	</module>
</application>


Download

Deploy ZkEJB3Demo.ear

Notice: before deploy the demo project, please make sure that the JBoss AS datasource is setup correctly. Refer to http://wiki.jboss.org/wiki/Wiki.jsp?page=ConfigDataSources

  • Stop JBoss Application Server
  • Copy ZkEJB3Demo.ear to JBOSS_HOME\server\default\deploy\
  • Start JBoss Application Server


Conclusion

ZK developres should have a general idea of ZK JpaUtil and OpenEntityManangerInView now. Using JpaUtil with OpenEntityManagerInView, ZK developers now are able to work ZK with Java Persistence Unit effortlessly. If you have any question, please feel free to leave comment here or post to ZK forum.




Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.
You got stuck here?
Let us know why!
For questions please use the forum