Use EJB 3.0 EntityManager API from Web Module

How-To : Using EJB 3.0 EntityManager API from Web Module

Date: 2/16/05
Author: Debu Panda

Introduction

This example application demonstrates Oracle's support for the EJB 3.0 specification using an Entity bean and further demonstrates the use of the EntityManager API for creating and finding bean instances. It provides an example of using the new EJB 3.0 components from a Web application.

EJB 3.0 greatly simplifies the development of EJBs, removing many complex development tasks. For example, creating a simple CMP entity EJB using EJB 2.1 requires a bean class and at least two interfaces, as well as a deployment descriptor. The remote (or local) and home interfaces had to extend javax.ejb.EJBObject and javax.ejb.EJBHome interfaces respectively, and the bean class had to implement the javax.ejb.EntityBean interface. However, in EJB 3.0, development is greatly simplified due to the following specifications:

  • The bean class can be a plain java class (POJO)
  • No interfaces are required for an entity bean
  • Annotations are used for O-R Mapping

This demonstration uses the Employee entity bean to demonstrate a entity bean using EJB 3.0.

Entity Bean example using EJB 3.0

 

The bean class is a plain java class that is annotated with @Entity to mark it as an entity bean.

@Entity
@Table(name = "EMP")
public class Employee implements java.io.Serializable
{
  private int empNo;
  private String eName;
  private double sal;

  @Id
  @Column(name="EMPNO", primaryKey=true)
  public int getEmpNo()
  {
    return empNo;
  }

..

}

The @Table annotation is used to specify the table name to be used by this Entity bean.

The @Id annotation is used to mark the empNo field as the primary key of the entity bean.

The @Column annotation is used to specify that the empNo field is mapped to the EMPNO column in the table.

Using EntityManager API from Web Module


The javax.persistence.EntityManager API is used for creating, finding and updating entity bean instances. Although the EJB 3.0 Early Release Draft 2 does not mandate the use of EntityManager API from web modules, the Oracle Application Server EJB 3.0 Preview does allow you to lookup and use an instance of EntityManager from a web module (Servlet/JSP).

The EntityManager API is required to be used within a transaction, so the web module must manually demarcate the transaction using the UserTransaction API.

In this example, the InsertServlet looks up the EntityManager instance and then creates entity bean instance as follows:

 

ut = (UserTransaction)initCtx.lookup("java:comp/UserTransaction");
ut.begin();
Context context = new InitialContext();
Employee employee = new Employee();
employee.setEmpNo(empId);
employee.setEname(name);
employee.setSal(sal);
EntityManager em = (EntityManager)context.lookup("java:comp/ejb/ejb30EMfromweb-ejb/EntityManager");
em.persist(employee);
ut.commit();

Web modules can lookup the EntityManager using the JNDI binding of java:comp/ejb/<ejb-module-name>/EntityManager

Prerequisites

What you need to know

In order to complete the example application, you should be familiar with the following:
  • EJB 2.1
  • EJB 3.0

For further information on EJB 3.0, see the following documents on OTN:

Software Requirements

This demonstration requires that the following software components are installed and configured correctly:

  • Oracle Application Server EJB 3.0 Preview
  • Sun JDK version 1.5 or above, available here
  • Apache Ant version 1.6.2 or above, available here
  • Any HTML browser like Mozilla, Microsoft Internet Explorer, Netscape, etc.
  • A relational database such as Oracle.

Notations

  • %ORACLE_HOME% - The directory where you installed the Oracle Application Server EJB 3.0 Preview
  • %JAVA_HOME% - The directory where your JDK is installed
  • %HOWTO_HOME% - The directory where this demo is unzipped

Building the Application

The Javadoc for this application is located in the %HOWTO_HOME%/doc/javadoc/ directory.

The configuration files are located in the %HOWTO_HOME%/etc directory, including deployment descriptor files such as application.xml.

Running the Application

To run the sample application on a standalone instance of Oracle Application Server EJB 3.0 Preview, follow these steps:

1. Examine the Sample File Directories

  • build - temporary directory created during the build
  • log - temporary directory holding build/deploy logs
  • etc - all necessary files to package the application
  • lib - holds the application archives that could be deployed
  • script - contains SQL script to create a table
  • doc - the How-to document and Javadoc's
    • javadoc - the javadoc of the different source files
    • how-to-ejb30-emAPIfromweb.html - this How-to page
  • src - the source of the demo
    • ejb - contains the sample entity bean code
    • web- contains application

2. Configure the Environment

Ensure the following environment variables are defined:

  • %ORACLE_HOME% - The directory where you installed OC4J.
  • %JAVA_HOME% - The directory where you installed the J2SE 5.0

Configure Database

This example is based on the EMP table from the SCOTT schema in an Oracle database. If you do not have SCOTT schema installed in your Oracle database, or are using a database other than Oracle, create the table using the table.sql script in the %HOWTO_HOME%/scripts directory.

Configure Data Source

This example requires a DataSource to be configured to connect to the database that contains the EMP table.

For OC4J, you must configure a datasource in the %ORACLE_HOME%/j2ee/home/config/data-sources.xml file and point it at the schema that owns the EMP table.

An example configuration:

<connection-pool name="ScottConnectionPool">
  <connection-factory factory-class="oracle.jdbc.pool.OracleDataSource"
   user="scott"
   password="tiger"
   url="jdbc:oracle:thin:@localhost:1521:ORCL" >
  </connection-factory>
</connection-pool>

<managed-data-source name="OracleManagedDS"
 connection-pool-name="ScottConnectionPool"
 jndi-name="jdbc/OracleManagedDS"
/> 

3. Start the EJB 3.0 Container

An OracleAS EJB 3.0 container must be running. Start the preview container using the following command:

>%ORACLE_HOME%/bin/ejb30 -start

4. Generate, Compile, and Deploy the Application

Ensure Ant 1.6.2 or above is installed on your machine and configured correctly. On some operating systems, Ant does not currently support the use of environment variables. If this is the case for your operating system, please modify the common.xml file located in the %HOWTO_HOME% directory.

Edit ant-oracle.properties (in the demo directory) and ensure the following properties are set to the correct values, as indicated below:

  • oc4j.host: host where OC4J is running (default localhost)
  • oc4j.admin.port: RMI port number (default 23791)
  • oc4j.admin.user: admin user name (default oc4jadmin)
  • oc4j.admin.password: admin user password (default welcome)
  • oc4j.binding.module: website name where deployed web modules are bound (default http-web-site)

To build the application, type the following command from the %HOWTO_HOME% directory:

>ant

You should now have the newly created ejb30entity.ear in your %HOWTO_HOME%/lib directory.

This command will attempt to deploy the application archive if the build is successful. It will first test whether OC4J is running before attempting the deployment operation.

Note that you can also deploy the application separately . Ensure the %ORACLE_HOME% environment variable is defined, and from the %HOWTO_HOME% directory, type the command:

>ant deploy

5. Run the Application

Run the sample by providing invoking the following URL from your favorite browser:

http://localhost:8888/ejb30EMfromweb

In this page, enter employee no, Name and Salary and then click on Add Employee button.

The InsertServlet will be invoked that will try to create the bean instance using EntityManager API.

You will be redirected to a success page if your record was inserted successfully. You can also check the database table to ensure the record was created.

Summary

In this document, you should have learned how to:

  • Develop a simple Entity bean using EJB 3.0
  • Use the EntityManager API from a web module
  • Deploy an execute the simple Entity bean using Oracle Application Server EJB 3.0 Preview

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy