Developing an EJB 3.0 entity in WebLogic Server

This book walks you through the practical usage of EJB 3.0 database persistence with Oracle Fusion Middleware. Lots of examples and a step-by-step approach make it a great way for EJB application developers to acquire new skills.

(For more resources on Oracle, see here.)

Setting the environment

In the following sections, we will learn how to set up the environment.

Installing required products

First, download and install the following required products; when installing the MySQL database, select the option to add the MySQL bin directory to the Windows system PATH environment variable:

  1. Oracle WebLogic Server 11g (
  2. Oracle Enterprise Pack for Eclipse All-In-One edition (
  3. MySQL 5.x database (

Creating a MySQL database table

Next, create a database table in the MySQL database as follows:

  • Log in to the MySQL database with the following command:


  • Set database as test:
    mysql>use test
  • Run the following SQL script, which creates a Catalog table for the EJB 3 entity:
    journal VARCHAR(100), publisher VARCHAR(100), date VARCHAR(100),
    title VARCHAR(100), author VARCHAR(100));

The output from the CREATE TABLE SQL script is shown in the following screenshot:

The table description may be listed with the desc command, as shown in the following illustration:

Configuring WebLogic Server with MySQL database

We shall be using a MySQL database for persistence. Therefore, we need to create a data source in WebLogic Server. Start the WebLogic Server and log in to the Administration Console.

Creating a data source

Select the base_domain | Services | JDBC | Data Sources. Click on New in the Data Sources table. Specify a data source name and a JNDI Name (jdbc/MySQLDS) for the data source. Select Database Type as MySQL and Database Driver as MySQL's Driver (Type 4): com.mysql.jdbc.Driver. Click on Next, as shown in the following screenshot:

(Move the mouse over the image to enlarge.)

In the Transaction Options window, select Supports Global Transactions and One-Phase Commit. Click on Next, as shown in the following screenshot:

Specify the connection properties: Database Name as test, Host Name as localhost, Port as 3306, and Database User Name as root. Specify the Password used when installing MySQL and click on Next, as shown in the following screenshot:

In the Test Database Connection window, the Driver Class Name and connection URL are specified, normally filled from the information you entered in the previous screen. Click on Test Configuration to test the connection. Click on Finish, as shown in the following screenshot:

A data source gets added to the Data Sources table with its data source JNDI Name as jdbc/MySQLDS, as shown in the following screenshot:

Deploying the data source

Next, we deploy the data source to a target server. Click on the data source link in the Data Sources table and select the Targets tab. Select the AdminServer checkbox and click on Save, as shown in the following screenshot:

The target server changes get applied and saved:

Testing the data source

To test the data source, click on Test Data Source. If the data source tests without an error, a message indicating the same gets displayed as shown next:

(For more resources on Oracle, see here.)

Creating a JPA project in Eclipse

For creating an EJB 3.0 entity bean we require Java Persistence API (JPA) project in Eclipse. Next, we create a JPA project in Eclipse. In the Eclipse IDE, select File | New. In the New window select JPA | JPA Project and click on Next, as shown below:

In the New JPA Project window, specify a Project name, select the default Contents directory, and select the Utility JPA project with Java 5.0. Click on Next:

In the New JPA Project window, select a JPA persistence provider under Platform. We shall be using the EclipseLink JPA persistence provider. We need a database connection for database persistence. Click on the Add connection link adjacent to the Connection select list, as shown in the following screenshot:

In the New Connection Profile window, select Connection Profile Type as MySQL, specify a connection profile name, and click on Next:

Next, specify a driver and other connection details. To add a new driver definition, click on the button adjacent to the Drivers select list, as shown next:

In the New Driver Definition window, select the Driver template as MySQL JDBC Driver System Version 5.1, and specify a Driver name (MySQLJDBCDriver). JAR files for the driver definition may need to be added, for which select the Jar List tab:

In the Jar List tab, add mysql-connector-java-5.1.10.jar, the MySQL 5.1 Connector-J JDBC JAR file, and click on OK.

The driver definition (MySQLJDBCDriver) gets added to the Drivers select list and may be selected for creating a connection profile. Specify the connection details in Properties: Database as test, URL as jdbc:mysql://localhost:3306/test, Username as root, and Password for the root user. Click on Test Connection to test the connection.

A Ping succeeded! message gets displayed if a connection gets established. Click on Next in the New Connection Profile window, as shown:

The Summary window displays the summary of the connection profile. Click on Finish, as shown next:

The connection profile gets added to the Connection select list. Next, select the JPA implementation. Select Use implementation library: EJB 3.0. In the Persistent class management section, select Annotated classes must be listed in persistence.xml. Click on Finish, as shown next:

An Open Associated Perspective? message prompt gets displayed. To open the JPA perspective, click on Yes.

A JPA project gets created and the JPA perspective gets opened. The Data Source Explorer view displays the database connections for the JPA project.

We need to have JRE 5.0 in the Build Path of the JPA project, as we shall be using JDK 5 features such as annotations. Right-click on the project node in Project Explorer and select Project Properties. In the Properties window, select the Java Build Path node and select the Libraries tab. Add a JRE 1.5 System library if not already added.

Creating an EJB 3.0 entity bean

In this section we create an EJB 3.0 entity bean. Select the JPA project node in Project Explorer and select File | New. In the New window, select JPA | Entity and click on Next.

In the New JPA Entity window, we define an Entity class. Select the project in which the Entity class is to be created. Select the Source folder, specify a Java package, and specify a Class name. In Inheritance, select Entity. Click on Next, as shown in the following screenshot:

In the Entity Properties window, the Entity Name, Table Name, and Entity Fields are specified. Entity fields may be added with the Add button. Click on Finish. An EJB 3.0 entity bean class gets added to the JPA project. The Entity class Catalog is shown with code, which we shall discuss next:

The EJB 3.0 entity class

Unlike EJB 2.0, in EJB 3.0 entity beans are Plain Old Java Objects (POJOs). Entity bean mappings are defined using annotations, which were introduced in JDK 5.0 and are in the javax.persistence package. A POJO class annotated with the @Entity annotation is an entity bean. The schema and table for the entity bean mapping is set at the class level using the @Table annotation. If the @Table annotation is not specified, the default table name is the entity bean class name. We shall create an entity bean Catalog that is mapped to Catalog table:

public class Catalog implements Serializable {

If an entity bean that has caching enabled is persisted to a database via an entity manager, the entity bean is serialized by caches. Therefore, an entity bean is recommended to implement the interface. In the entity bean class, specify the POJO properties. Also, specify the serialVersionUID, which is used by the serialization runtime to associate a version number with the serializable class. Add the getter and setter methods for the entity bean properties. Specify the identifier property with the @Id annotation. We have used only some of the EJB 3.0 annotations that may be specified in an entity bean. For a complete reference to EJB 3.0 annotations, refer to, EJB 3.0 specification ( The Catalog entity bean is listed next:

package ejb3;
import javax.persistence.*;
public class Catalog implements Serializable {
private static final long serialVersionUID = 7422574264557894633L;
private long id;
private String journal;
private String publisher;
private String date;
private String title;
private String author;
public Catalog(){ super();}
public Catalog(Integer id, String journal, String publisher,
String date, String title, String author){
public long getId() {
return id;
public void setId(long id) { = id;
public String getJournal() {
return journal;
public void setJournal(String journal) {
this.journal = journal; }
public String getPublisher() {
return publisher;
public void setPublisher(String publisher) {
this.publisher = publisher;
public String getDate() {
return date;
public void setDate(String date) { = date;

public String getTitle() {
return title;
public void setTitle(String title) {
this.title = title;
public String getAuthor() {
return author;
public void setAuthor(String author) { = author;


The open source combination of Eclipse-MySQL is ideally suited for developing EJB 3.0 entity beans with WebLogic Server 11g. An Eclipse-MySQL database is an open source alternative to a JDeveloper-Oracle database. In this article, we created an EJB 3.0 entity bean application in Oracle Enterprise Pack for Eclipse.

In the next article Configuring and Deploying the EJB 3.0 entity in WebLogic Server, we will learn to configure and deploy the EJB 3.0 entity.

Further resources on this subject:

Books to Consider

comments powered by Disqus

An Introduction to 3D Printing

Explore the future of manufacturing and design  - read our guide to 3d printing for free