Book Image

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Book Image

EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g

Overview of this book

EJB (Enterprise JavaBeans) 3.0 is a commonly used database persistence technology in Java EE applications. EJB 3.0 has simplified the development of EJBs with an annotations-based API that eliminates the use of remote/local interfaces, home/local home interfaces, and deployment descriptors. A number of other books are available on EJB 3.0, but none covers EJB 3.0 support in Oracle Fusion Middleware 11g, which is one of the leaders in the application server market.This is the first book that covers all aspects of EJB 3.0 database persistence development using Oracle Fusion Middleware technology. It covers all the best practices for database persistence ensuring that your applications are easily maintainable. Leaving theory behind, this book uses real-world examples to guide you in building your own EJB 3.0 applications that are well integrated with commonly used Java EE frameworks.The book gets going by discussing the new features in the EJB 3.0 specification. As some readers may still be using EJB 2.0, the book explains how to convert your EJB 2.0 entity beans to EJB 3.0. It then goes on to discuss using EJB 3.0 database persistence with JDeveloper, WebLogic Server, and Enterprise Pack for Eclipse, the main Java EE components of Oracle Fusion Middleware 11g. The book also covers EJB 3.0 relationships and integrating EJB 3.0 relationships with JSF user interfaces. EJB 3.0 database persistence with some of the commonly used frameworks such as ADF Faces, AJAX, and Web Services is also discussed in the book. It uses the integrated WebLogic Server 11g in some of the chapters and the standalone WebLogic Server in other chapters. While JDeveloper is the primary Java IDE used in the book, one of the chapters is based on the Oracle Enterprise Pack for Eclipse.By the time you reach the end of this book, you will be well-versed with developing EJB 3.0 applications using the different Java EE components of Oracle Fusion Middleware 11g.
Table of Contents (15 chapters)
EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g
Credits
About the Author
About the Reviewers
Preface

Simplified entity beans


An EJB 2.x Entity EJB bean class must implement the javax.ejb.EntityBean interface, which defines callback methods setEntityContext, unsetEntityContext, ejbActivate, ejbPassivate, ejbLoad, ejbStore, and ejbRemove that are called by the EJB container. An EJB 2.x provides implementation for the callback methods in the interface. An EJB 2.x entity bean also includes the ejbCreate and ejbPostCreate callback methods corresponding to one create method in the home interface. An EJB 2.x entity bean's component and home interfaces extend the EJBObject/EJBLocalObject and EJBHome/EJBLocalHome interfaces respectively. In comparison, an EJB 3.0 entity bean class is a POJO which does not implement the EntityBean interface. The callback methods are not implemented in the EJB 3.0 entity bean class. Also, the component and home interfaces and deployment descriptors are not required in EJB 3.0. The EJB configuration information is included in the Entity bean POJO class using metadata annotations. An EJB 2.1 entity bean also consists of getter/setter CMP (Container Managed Persistence) field methods, and getter/setter CMR (Container Managed Relationships) field methods. An EJB 2.x entity bean also defines finder and ejbSelect methods in the home/local home interfaces for EJB-QL queries. An example EJB 2.x entity bean is listed next:

import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
public class CatalogBean implements EntityBean {
private EntityContext ctx;
public abstract void setCatalogId();
public abstract String getCatalogId();
public abstract void setJournal();
public abstract String getJournal();
public String ejbCreate(String catalogId) {
setCatalogId(catalogId);
return null;
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void ejbLoad() {
}
public void ejbStore() {
}
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
public void unsetEntityContext() {
ctx = null;
}
}

In EJB 2.x, the ejb-jar.xml deployment descriptor defines the EJB-QL for finder methods. An example finder method is specified in the ejb-jar.xml as follows:

<query>
<query-method>
<method-name>findByJournal</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>
<![CDATA[SELECT DISTINCT OBJECT(obj) FROM Catalog obj WHERE obj.journal =
?1 ]]>
</ejb-ql>
</query>

An EJB 3.0 entity bean is a POJO class annotated with the @Entity annotation. The finder methods are specified in the entity bean class itself using the @NamedQuery annotation. The EJB 3.0 entity bean persistence annotations are defined in the javax.persistence package. Some of the EJB 3.0 persistence annotations are presented in the following table:

Annotation

Description

@Entity

Specifies an entity bean.

@Table

Specifies the entity bean table.

@SecondaryTable

Specifies a secondary table for an entity class for which data is stored across multiple tables.

@Id

Specifies an identifier property.

@Column

Specifies the database table column for a persistent entity bean property.

@NamedQueries

Specifies a group of named queries.

@NamedQuery

Specifies a named query or a query associated with a finder method.

@OneToMany

Specifies a one-to-many CMR relationship.

@OneToOne

Specifies a one-to-one CMR relationship.

@ManyToMany

Specifies a many-to-many CMR relationship.

The EJB 3.0 entity bean class corresponding to the EJB 2.x entity bean class is annotated with the metadata annotation @Entity. The finder method findByJournal in the EJB 2.x bean class is specified in the EJB 3.0 POJO class with the @NamedQuery annotation. The @Id annotation specifies the identifier property catalogId. The @Column annotation specifies the database column corresponding to the identifier property catalogId. If a @Column annotation is not specified for a persistent entity bean property, the column name is the same as the entity bean property name. Transient entity bean properties are specified with the @Transient annotation. The EJB 3.0 entity bean POJO class corresponding to the EJB 2.x entity bean is listed next:

import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.Id;
import javax.persistence.Column;
@Entity
@NamedQuery(name = "findByJournal", queryString = "SELECT DISTINCT OBJECT(obj) FROM Catalog obj WHERE obj.journal = ?1")
public class CatalogBean {
public CatalogBean() {
}
public CatalogBean(String catalogId) {
this.catalogId = catalogId;
}
private String catalogId;
private String journal;
@Id
@Column(name = "CatalogId", primaryKey = "true")
public String getCatalogId() {
return catalogId;
}
public void setCatalogId(String catalogId) {
this.catalogId = catalogId;
}
public void setJournal(String journal) {
this.journal = journal;
}
public String getJournal() {
return journal;
}
}

An EJB 2.x entity bean instance is created with the create() method in the entity bean home/local home interface. A client for an EJB 2.x entity bean obtains a reference for the entity bean with JNDI lookup; CatalogLocalHome is the JNDI name of the CatalogBean entity bean:

InitialContext ctx=new InitialContext();
Object objref=ctx.lookup("CatalogLocalHome");
CatalogLocalHome catalogLocalHome=(CatalogLocalHome)objref;
//Create an instance of Entity bean
CatalogLocal catalogLocal=(CatalogLocal)catalogLocalHome.create(catalogId);

To access the getter/setter methods of an entity bean, the remote/local object in EJB 2.x is obtained with the finder methods:

CatalogLocal catalogLocal =
(CatalogLocal) catalogLocalHome.findByPrimaryKey(catalogId);

An entity bean instance is removed with the remove() method:

catalogLocal.remove();

In EJB 3.0, persistence and lookup are provided by the EntityManger class. In a session bean client class for the EJB 3.0 entity bean, dependency injection is used to inject an EntityManager object using the @PersistenceContext annotation:

@PersistenceContext
private EntityManager em;

An entity bean instance is created by invoking new on the CatalogBean class and persisted with the persist() method of the EntityManager class:

CatalogBean catalogBean=new CatalogBean(catalogId);
em.persist(catalogBean);

An entity bean instance is obtained with the find() method:

CatalogBean catalogBean=(CatalogBean)em.find("CatalogBean", catalogId);

A Query object for a finder method is obtained with the createNamedQuery method:

Query query=em.createNamedQuery("findByJournal");

An entity bean instance is removed with the remove() method of the EntityManager class:

CatalogBean catalogBean;
em.remove(catalogBean);

The client class for the EJB 3.0 entity bean is listed next:

import javax.ejb.Stateless;
import javax.ejb.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
@Stateless
public class CatalogClient implements CatalogLocal {
@Resource
private EntityManager em;
public void create(String catalogId) {
CatalogBean catalogBean = new CatalogBean(catalogId);
em.persist(catalogBean);
}
public CatalogBean findByPrimaryKey(String catalogId) {
return (CatalogBean) em.find("CatalogBean", catalogId);
}
public void remove(CatalogBean catalogBean) {
em.remove(catalogBean);
}
}