How-To Make EJB 2.x interoperable with EJB 3.0

How-To Make EJB 2.x Interoperable with EJB 3.0

Date: 6/16/06
Author: Debu Panda

Introduction

This example application demonstrates Oracle's support for the interoperability between EJB 2.x and EJB 3.0; specifically it will demonstrate use of EJB 3.0 Persistence API from Stateless Session Bean written uisng EJB 2.x API.

EJB 3.0 will require the following:

  • Accessing EJB 3.0 Persistence API from EJB 2.x Beans
  • Accessing EJBs using 2.x Session and Entity beans from EJBs using EJB 3.0 API

To demonstrate interoperability, this example uses MySessionEJB (an EJB 2.x Session bean )to create Employee entity bean that uses EJB 3.0 Persistence API.

Entity Bean example using EJB 3.0

This example uses a EJB 3.0 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")
  public int getEmpNo()
  {
    return empNo;
  }

..

}

Using EntityManager API from EJB 2.x Stateless Session Bean

The javax.persistence.EntityManager API is used for creating, finding, updating entity bean instances. The MySessionEJB stateless session bean uses EntityManager API to create Employee bean instances. The stateless EJB makes a JNDI lookup to obtain an instance of EntityManager and uses persist method on EntityManager instance to create EJB 3.0 entity bean objects.

public class MySessionEJBBean implements SessionBean 
{
  
  public void addEmployee(int empNo, String eName, double sal)

  {
    Employee emp=null;      

    try 
    {
      Context ctx = new InitialContext();
      EntityManager em = (EntityManager) ctx.lookup("java:comp/env/HowToEntityManager");
	     
       if (emp == null) emp = new Employee();
      	emp.setEmpNo(empNo);
      	emp.setEname(eName); 
      	emp.setSal(sal);
      	em.persist(emp);
                
    }
    catch (Exception ex) 
    {
      ex.printStackTrace();
      System.out.println("Lookup failed");
    } 
        
    
  }

  public void ejbCreate()
  {
  }

..
}

Please note this release makes EntityManager available in ENC by using the persistence-context-ref. For packaging EJB 2.x beans with EJB 3.0 beans or entities you need to specify the version="3.0" in the ejb-jar tag in the deployment descriptor as follows:


<ejb-jar version="3.0">
<enterprise-beans>
<session>
<ejb-name>MySessionEJB</ejb-name>
..

<persistence-context-ref>
<persistence-context-ref-name>howto/EntityManager</persistence-context-ref-name>
<persistence-unit-name>howto</persistence-unit-name>
</persistence-context-ref>
</session>
</enterprise-beans>

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 10g 10.1.3.1
  • 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 Oracle's EJB 3.0 container.
  • %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 10g 10.1.3.1, 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-interoperability.html - this How-to page
  • src - the source of the demo
    • ejb - contains the sample SLSB code
    • client - contains application client code

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
  • %PATH% - includes %ORACLE_HOME% /ant/bin

Configure Database

This example requires EMP table in SCOTT schema in Oracle database. If you do not have SCOTT schema installed in your Oracle database or using a database other than Oracle the create the database table using table.sql in %HOWTO_HOME%/scripts directory.

Configure Data Source

This example requires the DataSource configured to connect to the database that contains the EMP table. Hence you need to configure your %ORACLE_HOME%/j2ee/home/config/data-sources.xml to the schema that owns the EMP table as follows:

<connection-pool name="Example Connection Pool">
<!-- This is an example of a connection factory that emulates XA behavior. -->
<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="Example Connection Pool"
jndi-name="jdbc/OracleDS"/>
 

3. Start the Server

Start OC4J stand alone using the following command after you make the above changes.

>%ORACLE_HOME%/bin/oc4j -start

If you are using an OracleAS managed install, start using the following command after you make the above changes.

> %ORACLE_HOME%/opmn/bin/opmnctl startall

4. Generate, Compile, and Deploy the Application

Ant 1.6.2 is shipped with OC4J and you have to set your PATH environment variable to $ORACLE_HOME/ant/bin. 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 ant-oracle.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 for OC4J standalone:

  • 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)

If you are using OracleAS managed install then you have appropriately change the following properties beside changing oc4j.admin.user and oc4j.admin.password for your managed OC4J instance in OracleAS install.

  • opmn.host: the hostname/IP where OracleAS is running (default localhost)
  • opmn.port: OPMN request port (default 6003) for the OracleAS install
  • oc4j.instance: admin user name (default oc4jadmin)

You have to uncomment appropriate deployer.uri in the ant-oracle.properties based on your environment i.e. a single instance OC4J or a clustered OC4J instance/group managed by OPMN.

You have to make changes in jndi.properties such as provider.url, principal and credential appropriate to your environment. If you are using OracleAS install, you have to use provider.url in the following format: opmn:ormi://localhost:6003:home/ejb30interoperejb2x.

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

>ant

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

This command would also attempt to deploy the application if the build is successful. It will first test whether OC4J is running.

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 the following command, including a name as the program argument:

ant run -Dempno=<empNo> -Dname=<empName> -Dsal=<salary>

e.g. ant run -Dempno=359 -Dname=Debu -Dsal=5000

You will get the following output generated by the Java client. You can also check the database table to ensure the record was created.

run:
[java] Employee with id:359 created

Summary

In this document, you should have learned how to:

  • Learn about EJB 3.0 interoperability with earlier releases of EJB
  • Use EntityManager API from EJB 2.x Session Bean
  • Deploy an application containing Simple EJB 3.0 Entity bean and EJB 2.x Session bean in the Oracle Application Server 10g 10.1.3 .1
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