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:
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>
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 demodirectory) 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