Developing an EJB 3.0 entity in WebLogic Server

Exclusive offer: get 50% off this eBook here
EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g — Save 50%

A complete guide to building EJB 3.0 database persistent applications with Oracle Fusion Middleware 11g tools with this book and eBook

$32.99    $16.50
by Deepak Vohra | August 2010 | Enterprise Articles Oracle

Developing Entity EJBs require an application server and a relational database, and, optionally, a Java IDE to improve productivity and simplify the development. Eclipse IDE is the most commonly used open source Java IDE and MySQL database is the most commonly used open source relational database. Oracle Enterprise Pack for Eclipse (OEPE) All-In-One edition bundles a pre-configured Eclipse and Eclipse plugins. Oracle has acquired the open source MySQL database. MySQL database is available under the GPL license; a commercial license is also available without the precondition to purchase support services from Oracle.

In this article by Deepak Vohra, author of EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g, we shall develop an EJB 3.0 entity using the Eclipse-WebLogic Server-MySQL combination; you will learn the following:

  • Creating a MySQL database table
  • Configuring WebLogic Server with MySQL database
  • Creating a JPA project in Eclipse
  • Creating an EJB 3.0 entity

(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 (http://www.oracle.com/technology/software/products/ias/htdocs/wls_main.html).
  2. Oracle Enterprise Pack for Eclipse All-In-One edition (http://www.oracle.com/technology/software/products/oepe/oepe_11113.html).
  3. MySQL 5.x database (http://www.oracle.com/us/products/mysql/index.html).

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:

    >mysql

  • Set database as test:
    mysql>use test
  • Run the following SQL script, which creates a Catalog table for the EJB 3 entity:
    CREATE TABLE Catalog (id INT PRIMARY KEY NOT NULL, 
    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:

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g A complete guide to building EJB 3.0 database persistent applications with Oracle Fusion Middleware 11g tools with this book and eBook
Published: August 2010
eBook Price: $32.99
Book Price: $54.99
See more
Select your format and quantity:

(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:

@Entity
@Table(name="Catalog")
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 java.io.Serializable 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 (http://java.sun.com/products/ejb/docs.html). The Catalog entity bean is listed next:

package ejb3;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="Catalog")
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){
super();
this.id=id;
this.journal=journal;
this.publisher=publisher;
this.date=date;
this.title=title;
this.author=author;
}
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.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) {
this.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) {
this.author = author;
}
}

Summary

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:


EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g A complete guide to building EJB 3.0 database persistent applications with Oracle Fusion Middleware 11g tools with this book and eBook
Published: August 2010
eBook Price: $32.99
Book Price: $54.99
See more
Select your format and quantity:

About the Author :


Deepak Vohra

Deepak Vohra is a consultant and a principal member of the NuBean.com software company. Deepak is a Sun Certified Java Programmer and Web Component Developer, and has worked in the fields of XML and Java programming and J2EE for over five years. Deepak is the co-author of the Apress book Pro XML Development with Java Technology and was the technical reviewer for the O'Reilly book WebLogic: The Definitive Guide. Deepak was also the technical reviewer for the Course Technology PTR book Ruby Programming for the Absolute Beginner, and the technical editor for the Manning Publications book Prototype and Scriptaculous in Action. Deepak is also the author of the Packt Publishing books JDBC 4.0 and Oracle JDeveloper for J2EE Development; Processing XML documents with Oracle JDeveloper 11g; EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g; and Java 7 JAX-WS Web Services.

Books From Packt


The Oracle Universal Content Management Handbook
The Oracle Universal Content Management Handbook

Oracle SOA Suite 11g R1 Developer's Guide
Oracle SOA Suite 11g R1 Developer's Guide

BPEL PM and OSB operational management with Oracle Enterprise Manager 10g Grid Control
BPEL PM and OSB operational management with Oracle Enterprise Manager 10g Grid Control

The Business Analyst's Guide to Oracle Hyperion Interactive Reporting 11
The Business Analyst's Guide to Oracle Hyperion Interactive Reporting 11

Oracle Coherence 3.5
Oracle Coherence 3.5

EJB 3 Developer Guide
EJB 3 Developer Guide

Oracle Siebel CRM 8 Installation and Management
Oracle Siebel CRM 8 Installation and Management

Oracle Database 11g – Underground Advice for Database Administrators
Oracle Database 11g – Underground Advice for Database Administrators


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
b
S
B
8
1
G
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software