5 min read

(For more resources on Oracle, see here.)

WebLogic server 10.x provides some value-added features to facilitate EJB 3 development. WebLogic server 10.x supports automatic deployment of a persistence unit based on the injected variable’s name. The @javax.persistence. PersistenceContext and @javax.persistence.PersistenceUnit annotation s are used to inject the persistence context in an EntityManager or EntityManagerFactory variable . A persistence context is a set of entities that are mapped to a database with a global JNDI name. If the name of the injected variable is the same as the persistence unit, the unitName attribute of the @PersistenceContext or @PersistenceUnit is not required to be specified. The EJB container automatically deploys the persistence unit and sets its JNDI name to be the same as the persistence unit name in persistence.xml. For example, if the persistence unit name in the persistence.xml file is em, an EntityManager variable may be injected with the persistence context as follows:

@PeristenceContext
private EntityManager em;

We did not need to specify the unitName attribute in the @PersistenceContext because the variable name is the same as the persistence unit. Similarly, an EntityManagerFactory variable may be injected with the persistence context as follows, emf being also the persistence unit name:

@PersistenceUnit
private EntityManagerFactory emf;

Another value-added feature in WebLogic server 10.x is support for vendor-specific subinterfaces of the EntityManager interface. For example, the BEA Kodo persistence provider provides the KodoEntityManager subinterface, which may be injected with the persistence context as follows:

@PersistenceContext
private KodoEntityManager em;

Setting the environment

Before getting started, we need to install Oracle JDeveloper 11g, which may be downloaded from http://www.oracle.com/technology/products/jdev/index.html. Download the Studio Edition, which is the complete version of JDevloper with all the features. Oracle JDeveloper 11g is distributed as a GUI self-extractor application. Click on the jdevstudio11110 install application. The Oracle Installer gets started. Click on Next in the Oracle Installer. Choose a middleware home directory and click on Next:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Choose the Install Type as Complete, which includes the integrated WebLogic Server, and click on Next:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Confirm the default Product Installation directories and click on Next:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

The WebLogic Server installation directory is the wlserver_10.3 folder within the middleware home directory. Choose a shortcut location and click on Next. The Installation Summary lists the products that are installed, which include the WebLogic Server and the WebLogic JDBC drivers. Click on Next to install Oracle JDeveloper 11g and the integrated WebLogic Server 10.3.

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

We also need to install the Oracle database 10g/11g or the lightweight Oracle XE, which may be downloaded from http://www.oracle.com/technology/software/products/database/index.html. When installing Oracle database, also install the sample schemas.

Creating a datasource in JDeveloper

Next, we create a JDBC datasource in JDeveloper. We shall use the datasource in the EJB 3.0 entity bean for database persistence. First, we need to create a database table in some sample schema, OE for example. Run the following SQL script in SQL *Plus:

CREATE TABLE Catalog (id INTEGER PRIMARY KEY NOT NULL,
journal VARCHAR(100), publisher VARCHAR(100), edition VARCHAR(100),
title VARCHAR(100), author VARCHAR(100));

A database table gets created in the OE sample schema.

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Next, we need to create a JDBC connection in JDeveloper with Oracle database. Open the Database Navigator or select the Database Navigator tab if already open. Right-click on the IDE Connections node and select New Connection:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

In the Create Database Connection window, specify a Connection Name, select Connection Type as Oracle (JDBC), specify Username as OE, which is the schema in which the Catalog table is created, and specify the password for the OE schema. Select Driver as thin, Host Name as localhost, SID as ORCL, and JDBC Port as 1521. Click on the Test Connection button to test the connection. If the connection gets established, click on OK:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

The OracleDBConnection gets added to the Database Navigator view. The CATALOG table that we created is listed in the Tables:

(Move the mouse over the image to enlarge.)

Creating an EJB 3 application

In this section, we create an EJB 3.0 application in JDeveloper. Select New Application:

Specify an Application Name, select the Java EE Web Application template, which consists of a Model project and a ViewController project, and click on Next:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Next, specify the name (EJB3ViewController) for the View and Controller project. In the Project Technologies tab, transfer the EJB project technology from the Available list to the Selected list using the > button. We have selected the EJB project technology, as we shall be creating an EJB 3.0 model. Click on Next:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Select the default Java settings for the View project and click on Next:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Configure the EJB Settings for the View project. Select EJB Version as Enterprise JavaBeans 3.0 and select Using Annotations. Click on Next. Next, create the Model project. Specify the Project Name (EJB3Model for example), and in the Project Technologies tab transfer the EJB project technology from the Available list to the Selected list using the > button. We have added the EJB project technology, as the EJB 3.0 application client is created in the View project. Click on Next:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Select the default Java settings for the Model project and click on Next:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Similar to the View project, configure the EJB settings for the Model project. Select EJB Version as Enterprise JavaBeans 3.0, select Using Annotations and click on Finish. As we won’t be using a jndi.properties file or an ejb-jar.xml file , we don’t need to select the generate option for the jndi.properties file and the ejb-jar.xml file:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

An EJB 3.0 application, which consists of a Model project and a ViewController project, get added in the Application tab:

Select the EJB3Model project in the Application navigator and select Tools | Project Properties. In the Project Properties window, select the Libraries and Classpath node. The EJB 3.0 library should be in the Classpath Entries:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Select the EJB Module node and select the OracleDBConnection in the Connection drop-down list. The datasource corresponding to the OracleDBConnection is jdbc/OracleDBConnectionDS.

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g


Subscribe to the weekly Packt Hub newsletter

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here