EJB3 Tomcat howto
The EJB3 specifications revealed a new persistence mechanism,
one of the most interesting aspects was that the new EJB3 persistence was released
as a standalone specification and was explicit in stating that EJB3 persistence
could be deployed outside a J2EE container. The EJB3 specification has a strong
similarity with Hibernate, a widely deployed
persistence framework. While hibernate is popular and has a large (vocal) following,
EJB3 is a standard which means that a developer has a choice of the implemention,
that code which follows established standards will generally be easier to maintain
and that a EJB persistence provider can be plugged in without having to rework large
sections of database code. EJB3 is an attractive framework in that the
meta-data required to map the Java objects to the database tables can be 'injected'
directly within the class definition (as opposed to using auxiliary xml files). The
purpose of this document is to demonstrate how to use EJB from within a web application
running inside Tomcat.
Requirements
EJB3 relies extensively on annotations which were a central enhancement of the
Java 1.5 (5.0) Tiger release so a 1.5 series JDK is mandatory. The Tomcat 5.5 releases are recommended as these have been
coded to take advantage of the new features in Java 1.5. A EJB3 provider is another necessity,
hibernate supply a provider although while writing (08/06/2006) it had not reached a stable
release. To demonstrate EBJ3 Oracle's toplink was selected because it is a mature product and now freely
available as part of the Glassfish project and is the
reference implementation for EJB3.
A zip file containing the necessary jar files (toplink and the J2EE apis) can be downloaded here
Configuration
The key to the simplicity of EJB3 is that a single configuration file is required.
persistence.xml is the central configuration file with a working example presented below.
The EJB3 specification clearly dictates where the class Persistence (which
has a static method to return a EntityManagerFactory) will look for the persistence.xml
file. In webapps it is within the WEB-INF/classes/META-INF directory (relative to the
doc. root of the webapp). As an XML document, peristence.xml is relatively simple,
the associated XML Schema defines 10 possible child nodes of the persistence-unit element and most
of these are optional.
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="persistencenam">
<provider>
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
</provider>
<class>com.company.beans.ClientBean</class>
<properties>
<property name="toplink.jdbc.driver" value="org.postgresql.Driver" />
<property name="toplink.jdbc.url" value="jdbc:postgresql://127.0.0.1/dbname" />
<property name="toplink.jdbc.user" value="dbuser" />
<property name="toplink.jdbc.password" value="dbpassword" />
</properties>
</persistence-unit>
</persistence>
A casual inspection of the code reveals that ClientBean is a simple POJO (Plain Old Java Object)
with annotations which provide the EJB3 provider with enough detail to map this object to the
client database table. Fundamental to any database, the 'Id' annotation supplies the identity
of the primary key to the EJB3 provider. The 'getters' and 'setters' are standard JavaBean
properties, a EJB3 provider will attempt to map these properties to columns using JavaBean naming
conventions and use reasonable type mappings. For example, getName and setName will
map to a database column name called 'name' with a type of varchar. In some situations
it may be required to explicitly provide a mapping, typically where the JavaBean properties
differ from the database column names, the 'Column' annotation can be used as seen with
the email property in ClientBean.
ClientBean.java source code
package com.company.beans;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persietence.Table;
@Entity
@Table(name="client")
public class ClientBean {
@Id
int id;
public void setId(int id) {
return id;
}
public int getId() {
return id;
}
String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Column(name="emailaddress")
String email;
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}
Client code
Once the bean has been compiled, using EJB3 is a very
simple process. A typical series of statements would be to use a the Persistence's static
createEntityManagerFactory factory method to return a EntityManagerFactory,
use this reference to generate a EntityManager and then use the EntityManager
for all other EJB3 operations (EntityManager is almost synonomous
A simple JSP code fragment is shown below to demonstrate EJB3 in action.
<%@ page import="javax.persistence.*,java.sql.*,javax.sql.*,java.util.*" %>
<%
EntityManagerFactory emf = Persistence.createEntityManagerFactory("gnetworks");
EntityManager em = emf.createEntityManager();
Query q = em.createNativeQuery("select id from client");
List l = q.getResultList());
>
The example presented above expresses how easy it is for EJB3 to be used from within
a web application. The simplicity of EJB3 combined with its standards based pedigree
will certainly be an incentive for a developer looking for a ERM solution to give
EJB3 some serious consideration.
|