Building an EJB 3.0 Persistence Model with Oracle JDeveloper

September 5, 2010

EJB 3.0

«»

Creating an EJB 3.0 entity bean

In this section we shall map an Oracle database table to an entity bean. Subsequently
we shall discuss the components of the entity bean class.


Mapping an Oracle database table to an entity bean


In this section, we create an EJB 3.0 entity bean from the Oracle database table
CATALOG that we created earlier. Select the EJB3Model project in the Application
navigator and select File | New. In the New Gallery window, select Categories |
Business Tier | EJB
and Items | Entities from Tables, and click on OK.



In the Persistence Unit, window, select New to create a new persistence unit. In the
New Persistence Unit window specify a persistence unit name (em). Specify JTA
DataSource Name
as jdbc/OracleDBConnectionDS, which is the datasource name
corresponding to the OracleDBConnection connection. Select the default settings for
Toplink: Server Platform as WebLogic 10. Click on OK.



The em Persistence Unit gets created. Click on OK in the Persistence Unit window.



Select Type of Connection as Online Database Connection and click on Next.



In the Database Connection Details window, select the OracleDBConnection and
click on Next. We had configured a connection earlier, but the database connection
can be created implicitly in the Database Connection Details, instead of explicitly.



In the Select Tables window, select Schema as OE, Name Filter as %, and check the
Auto Query checkbox. Select the CATALOG table and click on Next.



Select the default settings in the General Options window. The default package name
is model. In the Entity Class, select Place member-level annotations on as Fields, and
select the Implement java.io.Serializable checkbox. Click on Next.



In the Specify Entity Details window, select Table Name as OE.CATALOG. As
shown in the following screenshot, specify Entity Name as Catalog and Entity Class
as model.Catalog. Click on Next.



The Summary page lists the EJB 3.0 JPA Entity that will be generated. In the
Summary Page, click on Finish.



The CMP Entity bean class—model.Catalog—gets created. The persistence.xml
deployment descriptor gets created in the META-INF directory.



The entity bean class


The entity bean class is just a POJO class annotated with the @Entity annotation .
A @NamedQuery specifies a findAll query , which selects all the entity instances.
An entity bean that has caching enabled is persisted to a database; the entity bean
is serialized by caches. Therefore, the entity bean class implements the java.
io.Serializable interface . Specify a serialVersionUID variable that is used by
serialization runtime to associate a version number with the serializable class:



private static final long serialVersionUID = 7422574264557894633L;


The database columns are mapped to entity bean properties, which are defined as
private variables. The getter setter methods for the properties are also defined. The
identifier property is specified with the @Id annotation. The @Column annotation
specifies that the id column is not nullable:



@Id
@Column(nullable = false)
private long id;


By default the id column of type INTEGER is mapped to a field of type Long. Modify
the id field to type long, as usually id values are of primitive type. The entity bean
class is listed next:



package model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
@NamedQueries({
@NamedQuery(name = “Catalog.findAll”, query = “select o from Catalog
o”)
})
public class Catalog implements Serializable {
private String author;
private String edition;
private static final long serialVersionUID = 7422574264557894633L;
@Id
@Column(nullable = false)
private long id;
private String journal;
private String publisher;
private String title;
public Catalog() {super();
}
public Catalog(String author, String edition, long id, String
journal,
String publisher, String title) {
super();
this.author = author;
this.edition = edition;
this.id = id;
this.journal = journal;
this.publisher = publisher;
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
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 getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}


The persistence.xml file is used to define the persistence unit/s, which include
a JTA datasource that is used for database persistence. The persistence provider is
specified as org.eclipse.persistence.jpa.PersistenceProvider. The jtadata-
source is defined as java:/app/jdbc/jdbc/OracleDBConnectionDS. The
eclipselink.target-server property is specified as WebLogic_10. The javax.
persistence.jtaDataSource property is specified as java:/app/jdbc/jdbc/
OracleDBConnectionDS, which is just the default mapping JDeveloper uses for the
JTA Data Source. The java:/app/jdbc prefix gets added to the JTA Data Source
specified when creating the persistence unit. The persistence.xml configuration
file is listed next:



<?xml version=”1.0″ encoding=”windows-1252″ ?>
<persistence xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence

http://java.sun.com/xml/ns/persistence/

persistence_1_0.xsd”
version=”1.0″ xmlns=”http://java.sun.com/xml/ns/persistence”>
<persistence-unit name=”em”>
<provider>
org.eclipse.persistence.jpa.PersistenceProvider
</provider>
<jta-data-source>
java:/app/jdbc/jdbc/OracleDBConnectionDS
</jta-data-source>
<class>
model.Catalog
</class>
<properties>
<property name=”eclipselink.target-server” value=”WebLogic_10″
/>
<property name=”javax.persistence.jtaDataSource”
value=”java:/app/jdbc/jdbc/OracleDBConnectionDS” />
</properties>
</persistence-unit>
</persistence>

EJB 3.0 Articles

email

«»

Comments

comments