Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA

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.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 jdev rundebug

< <>>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.>

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 jdev editrunconfig

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.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 jdev libraries

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.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 jdev connections

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.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 cmp wizard

The wizard asks us which EJB version we are working with. Pick Enterprise Java Beans 3.0 (J2EE 5.0) (the default).

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 ent wiz1

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.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 ent wiz2

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 ent wiz3

Next is another page with some general settings – the package to generate the Entities into for example.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 ent wiz4

Finally there is the Summary Page with that Magnificent Finish button. Press it.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 ent wiz5

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:

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 afterentwiz

 

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.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 beanwiz1

Specify the name of the EJB as well as the type: Stateless Bean Managed.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 beanwiz2

The next page has us defining the exact package and class name for the business service class:

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 beanwiz3

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.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 beanwiz4

 

 The summary:

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 beanwiz5

 

After pressing Finish, we get:

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 afterbeanWiz

 

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!

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 hrmservice

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:

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 hrmclient

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.

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA

Now we can run the HrmClient application, simply by selecting that class and clicking on the Run icon. The output looks like:

Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA ejb30 runclient

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.

4 Comments

  1. Keshava Rangarajan January 3, 2006
  2. Keshava Rangarajan January 3, 2006
  3. Keshava Rangarajan January 3, 2006
  4. eduardo ivan pichler December 28, 2005