This example application
demonstrates Oracle's support for the EJB
3.0 specification with an Entity Bean and
further demonstrates the use of the EntityManager API for creating and finding
bean instances.
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 atleast 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
The javax.persistence.EntityManager API is used for creating, finding, updating entity bean instances. The EmployeeFacade session bean uses EntityManager API to create and find bean instances. You can inject an instance of EntityManager and use persist or find method on EntityManager instance to create or query entity bean objects.
@Stateless
public class EmployeeFacadeBean implements EmployeeFacade
{
@Resource
private EntityManager em;
private Employee emp;
public Employee findEmployeeByEmpNo(int empNo)
{
return ((Employee) em.find("Employee",empNo));
}
public void addEmployee(int empNo, String eName, double sal)
{
if (emp == null) emp = new Employee();
...
em.persist(emp);
}
...
}
Prerequisites
What you need to know
In order to complete the example application, you should
be familiar with the following:
EJB 3.0
For further information on EJB 3.0, see the following
documents on OTN:
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 Application
Server 10g 10.1.3
%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, ejb-jar.xml, etc.
Running the Application
To run the sample application on a standalone instance
of Oracle Application Server 10g 10.1.3, follow these steps:
1. Examine
the Sample File Directories
build - temporary directory created during the build
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-entity.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 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 usingthe 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 OC4J 10g 10.1.3 instance must
be running. Start the container using the following command:
%ORACLE_HOME%/bin/oc4j -start
4. Generate, Compile,
and Deploy the Application
Ensure $ORACLE_HOME/ant/bin is included in your PATH environment variable. 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 demodirectory) 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 also attempt to deploy the application to the defined OC4J
if the build is successful. It will first test whether OC4J is running before
attempting this task.
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>
For example, run the sample with the following arguments to create a new record
with the specified values:
>ant run -Dempno=358 -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 empNo:358 created
[java] Find the employee using Entity Manager API
[java] EmployeeBean Details
[java] Class:oracle.ejb30.Employee :: empNo:358
ename:Debu sal:5000.0
Summary
In this document, you should have learned how to:
Develop an Entity bean using EJB
3.0 and JDK 5.0 annotations
Use the EntityManager API to create and find Entity beans
Deploy and execute an Entity bean with
the Oracle Application Server 10g 10.1.3