Configuring and Deploying the EJB 3.0 Entity in WebLogic Server

This book walks you through the practical usage of EJB 3.0 database persistence with Oracle Fusion Middleware. Lots of examples and a step-by-step approach make it a great way for EJB application developers to acquire new skills.

(For more resources on Oracle, see here.)

Creating a Persistence Configuration file

An EJB 3.0 entity bean is required to have a persistence.xml configuration file, which defines the database persistence properties. A persistence.xml file gets added to the META-INF folder when a JPA project is defined. Copy the following listing to the persistence.xml file in Eclipse:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi=""
xmlns="" version="1.0">
<persistence-unit name="em">
<property name="" value="WebLogic_10"
<property name="javax.persistence.jtaDataSource" value="jdbc/
<property name="eclipselink.ddl-generation"
value="create-tables" />
<property name="" value="MySQL" />

The persistence-unit is required to be named and may be given any name. We had configured a JDBC data source with JNDI jdbc/MySQLDS in WebLogic Server. Specify the JNDI name in the jta-data-source element. The properties element specifies vendor-specific properties. The eclipselink.ddl-generation property is set to create-tables, which implies that the required database tables will be created unless they are already created . The persistence.xml configuration file is shown in the Eclipse project in the following illustration:

(Move the mouse over the image to enlarge.)

Creating a session bean

For better performance, one of the best practices in developing EJBs is to access entity beans from session beans. Wrapping an entity bean with a session bean reduces the number of remote method calls as a session bean may invoke an entity bean locally. If a client accesses an entity bean directly, each method invocation is a remote method call and incurs an overhead of additional network resources. We shall use a stateless session bean, which consumes less resources than a stateful session bean, to invoke entity bean methods. In this section, we create a session bean in Eclipse. A stateless session bean class is just a Java class annotated with the @Stateless annotation. Therefore, we create Java classes for the session bean and session bean remote interface in Eclipse. To create a Java class, select File | New. In the New window, select Java | Class and click on Next>

In the New Java Class window, select the Source folder as EJB3JPA/src, EJB3JPA being the project name. Specify Class Name as CatalogTestBean and click on Finish.

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Similarly, create a CatalogTestBeanRemote interface by selecting Java | Interface in the New window. The session bean class and the remote interface get added to the EJB3JPA project.

The session bean class

The stateless session bean class, CatalogTestBean implements the CatalogTestRemote interface. We shall use the EntityManager API to create, find, query, and remove entity instances. Inject an EntityManager using the @PersistenceContext annotation. Specify unitName as the same as the persistence-unit name in the persistence.xml configuration file:

@PersistenceContext(unitName = "em")
EntityManager em;

Next, create a test() method, which we shall invoke from a test client. In the test() method we shall create and persist entity instances, query an entity instance, and delete an entity instance, all using an EntityManager object, which we had injected earlier in the session bean class. Injecting an EntityManager implies that an instance of EntityManager is made available to the session bean. Create an instance of the Entity bean class:

Catalog catalog = new Catalog(new Integer(1), "Oracle Magazine",
"Oracle Publishing", "September-October 2009",
"Put Your Arrays in a Bind","Mark Williams");

Persist the entity instance to the database using the persist() method:


Similarly, persist two more entity instances. Next, create a query using the createQuery() method of the EntityManager object. The query string may be specified as a EJB-QL query. Unlike HQL, the SELECT clause is not optional in EJB-QL. Execute the query and return the query result as a List using the getResultList() method. As an example, select the catalog entry corresponding to author David Baum. The FROM clause of a query is directed towards the mapped entity bean class, not the underlying database.

List catalogEntry =em.createQuery("SELECT c from Catalog c where").setParameter("name","David Baum").

Iterate over the result list to output the properties of the entity instance:

for (Iterator iter = catalogEntry.iterator(); iter.hasNext(); ) {
Catalog element = (Catalog);
retValue =retValue + "<br/>" + element.getJournal() +
"<br/>" + element.getPublisher() +"<br/>" +
element.getDate() + "<br/>" + element.getTitle() +
"<br/>" + element.getAuthor() +"<br/>";

The variable retValue is a String that is returned by the test() method. Similarly, create and run a EJB-QL query to return all titles in the Catalog database:

List allTitles =em.createQuery("SELECT c from Catalog c").

An entity instance may be removed using the remove() method:


The corresponding database row gets deleted from the Catalog table. Subsequently, create and run a query to list all the entity instances mapped to the database. The session bean class, CatalogTestBean, is listed next:

package ejb3;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
* Session Bean implementation class CatalogTestBean
@Stateless(mappedName = "EJB3-SessionEJB")
public class CatalogTestBean implements CatalogTestBeanRemote {
@PersistenceContext(unitName = "em")
EntityManager em;

* Default constructor.
public CatalogTestBean() {
// TODO Auto-generated constructor stub

public String test() {
Catalog catalog = new Catalog(new Integer(1), "Oracle Magazine",
"Oracle Publishing", "September-October 2009",
"Put Your Arrays in a Bind","Mark Williams");
Catalog catalog2 = new Catalog(new Integer(2), "Oracle Magazine",
"Oracle Publishing", "September-October 2009",
"Oracle Fusion Middleware 11g: The Foundation for Innovation",
"David Baum");
Catalog catalog3 = new Catalog(new Integer(3), "Oracle Magazine",
"Oracle Publishing", "September-October 2009",
"Integrating Information","David Baum");
String retValue = "<b>Catalog Entries: </b>";
List catalogEntry = em.createQuery("SELECT c from Catalog c
"David Baum").getResultList();
for (Iterator iter = catalogEntry.iterator(); iter.hasNext(); ) {
Catalog element = (Catalog);
retValue = retValue + "<br/>" + element.getJournal() + "<br/>" +
element.getPublisher() + "<br/>" + element.getDate()
+ "<br/>" + element.getTitle() + "<br/>" +
element.getAuthor() + "<br/>";
retValue = retValue + "<b>All Titles: </b>";
List allTitles =
em.createQuery("SELECT c from Catalog c").getResultList();
for (Iterator iter = allTitles.iterator(); iter.hasNext(); ) {
Catalog element = (Catalog);
retValue = retValue + "<br/>" + element.getTitle() + "<br/>";
em.remove(catalog2); );
retValue = retValue + "<b>All Entries after removing an entry:
List allCatalogEntries =
em.createQuery("SELECT c from Catalog c").
for (Iterator iter = allCatalogEntries.iterator(); iter.hasNext();
) {
Catalog element = (Catalog);
retValue = retValue + "<br/>" + element + "<br/>";
return retValue;

We also need to add a remote or a local interface for the session bean:

package ejb3;
import javax.ejb.Remote;
public interface CatalogTestBeanRemote {
public String test();

The session bean class and the remote interface are shown next:

We shall be packaging the entity bean and the session bean in a EJB JAR file, and packaging the JAR file with a WAR file for the EJB 3.0 client into an EAR file as shown next:

EAR File
|-WAR File
|-EJB 3.0 Client
|-JAR File
|-EJB 3.0 Entity Bean
EJB 3.0 Session Bean

Next, we create an application.xml for the EAR file. Create a META-INF folder for the application.xml. Right-click on the EJB3JPA project in Project Explorer and select New>Folder. In the New Folder window, select the EJB3JPA folder and specify the new Folder name as META-INF. Click on Finish. Right-click on the META-INF folder and select New | Other. In the New window, select XML | XML and click on Next. In the New XML File window, select the META-INF folder and specify File name as application.xml. Click on Next. Click on Finish.

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

An application.xml file gets created. Copy the following listing to application.xml:

<?xml version = '1.0' encoding = 'windows-1252'?>

The application.xml in the Project Explorer is shown next:

(For more resources on Oracle, see here.)

Creating a test client

We have created an entity bean that contains the business logic. We have created a session bean that is a wrapper for the entity bean. Next, we shall create a test client JSP in which we shall invoke the test() method of the session bean. For the web application component, we need to create a web module folder, a WEB-INF folder in the web module folder and a web.xml in the WEB-INF folder. To create a JSP in the webModule folder, right-click on the webModule folder and select New | Other.

In the New window, select Web | JSP and click on Next.

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

In the New Java Server Page window, select the webModule folder and specify the JSP File name as catalog.jsp. Click on Finish, as shown next:

The catalog.jsp JSP gets added to the webModule folder. The catalog.jsp is shown below, including the JSP code, which we shall discuss next:

We shall create an instance of CatalogTestRemote using JNDI lookup for which we need to create an IntialContext object. We need to set the InitialContext environment using the environment properties:

InitialContext context = new InitialContext();

Obtain a CatalogTestBeanRemote instance using remote JNDI lookup on the session bean remote object. The JNDI name for WebLogic server is of the format mapped_name#remote_interface_class_name.

CatalogTestBeanRemote beanRemote = (CatalogTestBeanRemote) context.

Invoke the test() method of the session bean and output the String returned, as shown next:

String catalog=beanRemote.test();

The test client JSP is listed next:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@ page import="ejb3.*, javax.naming.*" %>
<meta http-equiv="Content-Type" content="text/html;
<title>Insert title here</title>
<body><% InitialContext context = new InitialContext();
CatalogTestBeanRemote beanRemote = (CatalogTestBeanRemote)
String catalog=beanRemote.test(); %><%=catalog %>

Deploying the entity in WebLogic Server

We have created all the classes and configuration files we need to create an EJB 3.0 entity bean. Next, we shall compile the classes to create a EJB JAR file. We shall create a WAR file from the EJB 3.0 client JSP and package the WAR file with the JAR file into an EAR file. We shall use a build.xml script to compile the EJB classes, create an EAR file, and deploy the EAR file to WebLogic Server. Create a build.xml script in the JPA project with File | New | Other and XML | XML in the New window. The build.xml script is shown next:

In the build script, specify properties for the various directory paths used in the script, such as the WebLogic server directory, the build directory, and the WebLogic Server deploy directory. Specify the classpath of the various JAR files required for compiling the EJB classes. Specify targets discussed in the following table:




Creates the build directory.


Compiles the EJB classes.


Creates an EJB JAR file.


Creates a WAR file.


Creates an EAR file.


Deploys the EAR file to WebLogic Server. The WebLogic Server deploy directory development mode is ${weblogic.home}/user_projects/domains/ base_domain/autodeploy


Deletes the build directory and the EJB JAR, WAR and EAR files.


The build.xml script is listed next:

<?xml version="1.0" encoding="UTF-8"?>
WebLogic build file
<project name="EJB3EntityBean" default="deploy" basedir=".">
<property environment="env" />
<property name="src.dir" value="${basedir}/src" />
<property name="web.module" value="${basedir}/webModule" />
<property name="weblogic.home" value="C:/Oracle/Middleware/wls" />
<property name="weblogic.server"
value="${weblogic.home}/wlserver_10.3/server" />
<property name="build.dir" value="${basedir}/build" />
<property name="deploy.dir"
autodeploy" />
<path id="classpath">
<fileset dir="${weblogic.home}/modules">
<include name="*.jar" />
<fileset dir="${weblogic.server}/lib">
<include name="*.jar" />
<pathelement location="${build.dir}" />
<property name="build.classpath" refid="classpath" />
<target name="prepare">
<mkdir dir="${build.dir}" />
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${build.dir}" debug="on"
<classpath refid="classpath" />
<target name="jar" depends="compile">
<jar destfile="${build.dir}/ejb3.jar">
<fileset dir="${build.dir}">
<include name="**/*.class" />
<fileset dir="${src.dir}/">
<include name="META-INF/persistence.xml" />
<target name="war" depends="jar">
<war warfile="${build.dir}/weblogic.war">
<fileset dir="webModule">
<include name="*.jsp" />
<fileset dir="webModule">
<include name="WEB-INF/web.xml" />
<target name="assemble-app" depends="war">
<jar jarfile="${build.dir}/ejb3.ear">
<metainf dir="META-INF">
<include name="application.xml" />
<fileset dir="${build.dir}" includes="*.jar,*.war" />
<target name="deploy" depends="assemble-app">
<copy file="${build.dir}/ejb3.ear" todir="${deploy.dir}" />
<target name="clean">
<delete file="${build.dir}/ejb3.ear" />
<delete file="${build.dir}/ejb3.jar" />
<delete file="${build.dir}/weblogic.war" />

Next, run the build script. Right-click on the build.xml script in the Package Explorer and select Run As | Ant Build (the second Ant Build), as shown in the following screenshot:

In the Edit Configuration window, select the target(s) to run. The default build target in the build script is deploy with each target having a dependence on the preceding target. The prepare, compile, jar, war, and assemble-app targets run when the deploy target is run. Select the deploy target and click on Apply. Click on Run to run the deploy target and thus deploy the EAR file to the WebLogic Server autodeploy directory.

All the targets get run and the EAR file gets deployed to the WebLogic Server, as shown in the build script output:

Start the WebLogic Server, if not already started, and log in to the Administration Console. Navigate to the Deployments node. The EAR file is shown as deployed in the Deployments table:

Testing the EJB 3.0 entity client

Next, run the client JSP in a browser with the URL http://localhost:7001/weblogic/catalog.jsp. The test() method of the session bean gets invoked. Three entity bean instances get created and persisted to the MySQL database. As shown in the server output, the entity bean property values for the entity instance corresponding to author David Baum get listed. The titles of all the three entity instances get listed. Subsequently, an entity instance gets removed, as indicated by the subsequent query to fetch all entity instances, which lists only two entity instances.

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g


In this article, we created a wrapper session bean in Oracle Enterprise Pack for Eclipse, packaged the application into an EAR file, and deployed the application to WebLogic Server using a build script. Subsequently, we ran the application in WebLogic server with a MySQL database as the EJB 3.0 persistence database.

Further resources on this subject:

Books to Consider

comments powered by Disqus

An Introduction to 3D Printing

Explore the future of manufacturing and design  - read our guide to 3d printing for free