In a previous article –Getting Started with EJB 3.0 Persistence out-of-container using the Reference Implementation (GlassFish) – I have introduced GlassFish, the official Reference Implementation for EJB 3.0, and how the EJB 3.0 Persistence implementation from GlassFish can be used. In this post, I want to focus on using GlassFish’s EJB 3.0 Persistence Implementation with Oracle JDeveloper 10.1.3 (EA). We will see how to set JDeveloper up to get it working and then we will see how to take advantage of some of the JDeveloper wizards for generating stub code and EJB 3.0 meta-data to quickly get us going. For instructions on the downloading and installing GlassFish, please see the post mentioned above. For downloading and installing JDeveloper 10.1.3EA, go to the JDeveloper Homepage on Oracle Technology Network.
Configuring the EJB 3.0 Persistence JDeveloper environment
Our first challenge is to set up a new JDeveloper Workspace and Project for our EJB 3.0 Persistence application, using only the GlassFish libraries, no Oracle specific libraries. The steps are:
Open JDeveloper 10.1.3EA. Create a new application – I call it ejb30_se. Select No Template for the Application Template. Press OK. Create a project. I called it: hrmPersistence.
Now it starts to get interesting: go to Project Properties – for example from the Project Node, rmb menu. Go to the Run/Debug tab and press on Edit for the Default Run Configuration.
< <>>There are three important things to do on this Edit Configuration Tab: set the Virtual Machine to Client. This has to do with the javaagent option that we will set next: in the Java Options input field, enter: -javaagent:.\toplink-essentials-agent.jar. In the Run Directory enter the $Glassfish_Home\lib directory.>
Next, go to the Libraries Tab. Here we specify which jars to include on the Classpath when we compile and run our application. The first three jars are taken from the GlassFish installation and represent the Reference Implementation for the EJB 3.0 javax.persistence interfaces. Oracle JDBC is a built in JDeveloper library with jars for the Oracle (JDBC) Driver.
Let’s start to flesh out the application itself. Using two JDeveloper wizards, we can get a long way without writing any code.
Create the HRM application based on EJB 3.0 Persistence
I first create a new Database Connection – of course to my local Scott Schema.
Then it’s time to start using a wizard. Go to the New Gallery, the Business Tier, EJB node. Select the Item CMP Entity Beans from Tables. This will create annotated POJOs: entities (or domain classes) that contain the required mapping information in EJB 3.0 format, directly derived from the existing database tables. It is not exactly the pure ‘let’s do some UML modelling first and worry about database implementation later’ approach, but given an existing database, it is very pragmatical.
The wizard asks us which EJB version we are working with. Pick Enterprise Java Beans 3.0 (J2EE 5.0) (the default).
The wizard then prompts us for the Database Connection (Scott Local) and let’s us choose the table to create the Entities for: EMP and DEPT. You can set the names for the Entities – by default they are the same as the Table Names.
Next is another page with some general settings – the package to generate the Entities into for example.
Finally there is the Summary Page with that Magnificent Finish button. Press it.
The wizard generates two Entities for us, classes Employee and Department. POJOs with all properties, getters and setters for the columns in tables EMP and DEPT. And with all EJB 3.0 annotations to instruct EntityManagers on persisting these entities to the underlying database tables:
You can now immediately start writing your own code, directly against the javax.persistence API for EntityManagerFactory and EntityManager, manipulating Employees and Departments. However, it is much neater and better structured to create a Business Service that provides methods – services – for the manipulations and special queries that the application may desire. This Business Service will make use of the javax.persistence API, but the application itself does not. So we are aiming for a HrmService class that provides these services to applications interested in working with Employees and Departments. It is like an out-of-container Session Bean implementing a Facade.
Creating such a Business Service object is simple in JDeveloper. Use the EJB Session Bean wizard to generate a Session Bean. We can finetune this session bean, but it is a perfect no-hand-code starting point for our HrmService class. Go to New, Business Tier, EJB and select Session Bean.
Specify the name of the EJB as well as the type: Stateless Bean Managed.
The next page has us defining the exact package and class name for the business service class:
On the final page – really no relevant for our purpose as we will use the generated bean outside the EJB Container – we have to choose for Remote and/or Local Interface.
The summary:
After pressing Finish, we get:
We have class HrmService that provides the core service methods for the Entities Employee and Department. We can use these methods to retrieve and manipula
te the entities and the HrmService will interact with the EntityManager on our
behalf.
There is one thing though that we must change: the setEntityManager method is annotated with @Resource. That is an instruction to the EJB Container to inject the actual EntityManager instance. Clearly, since our application is a J2SE stand alone out of container application, we need to inject the EntityManager manually. Note: the HrmServiceLocal interface that was generated by the wizard is completely irrelevant for our application. The next screenshot demonstrates how the HrmService class is changed, to set the EntityManager during instantiation. Of course this hard coded solution is not appropriate for a real application!
I have also created the HrmClient, the application that leverages our HrmService and the persistency underneath it, for some very simple purposes: find employees, create a department and search for the department:
Before we can run the application, we have to create the persistence.xml file in the src\META-INF directory of our project. This file contains the configuration details for our EntityManager, such as the database connection and jdbc driver we are using. Note: these details can also be passed to the EntityManagerFactory in a Map, in a purely programmatical way.
Now we can run the HrmClient application, simply by selecting that class and clicking on the Run icon. The output looks like:
Note: creating this application and getting it to run took less than one hour! And that includes figuring out the correct project settings etc. Once you have done it, the next time it will be less than 30 minutes. Not bad for a simple application that leverages quite a complex mechanism underneath!
Resources
Download the Oracle JDeveloper 10.1.3 EA1 Application Workspace and Project with all sources described above: ejb30_se.zip.
Lucas
One more minor point, you might have noticed that since the location Id in the HR
schema was a 4 digit number, I had to change the
service and the client code to insert a long.
Hence in HrmService.java the createDept method looks as follows:
public Department createDept(Long deptno, String dname,
Long loc) {
final Department dept = new Department();
dept.setDeptno(deptno);
dept.setDname(dname);
dept.setLocation(loc);
getEntityManager().persist(dept);
return dept;
}
and in HrmClient.java, the create line looks as follows:
hrmService.createDept(new Long(280),”New Dept”, new Long(1800));
sincerely,
k7
Oops sorry typo! Meant to say Oracle Express Database not Epress database!
k7
Lucas,
Thanks a lot for your excellent example. Just got your example working in JDev
with the Epress database using the sample HR schema.
Needed a few minor changes to make it happen. Here they are:
1. In the persistence.xml file the value to the connection string:
2. The JDBC user and password (of course)
2. The persistence mapping entries for the persistent objects:
In Department.java
@Table(name=”DEPARTMENTS”)
public class Department implements Serializable {
@Id
@Column(name=”DEPARTMENT_ID”)
public Long getDeptno() {
@Column(name=”DEPARTMENT_NAME”)
public String getDname() {
@Column(name=”LOCATION_ID”)
public Long getLocation() {
In Employee.java
@Table(name=”EMPLOYEES”)
public class Employee implements Serializable {
@Column(name=”COMMISSION_PCT”)
public Double getComm() {
@Column(name=”DEPARTMENT_ID”)
public Long getDeptno() {
@Id
@Column(name=”EMPLOYEE_ID”, nullable=false)
public Long getEmpno() {
@Column(name=”LAST_NAME”)
public String getEname() {
@Column(name=”HIRE_DATE”)
public Timestamp getHiredate() {
@Column(name=”JOB_ID”)
public String getJob() {
@Column(name=”MANAGER_ID”)
public Long getMgr() {
@Column(name=”SALARY”)
public Double getSal() {
I’ll now start playing around with the mappings. Thanks again!
sincerely,
k7
Congratulations, this and the before article is the better of category jdeveloper + ejb3
He is very helpful for a begginer like me.
Thanks