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 Session Beans


In EJB 2.x, a session bean is required to implement the SessionBean interface. An EJB 3.0 session bean class is a POJO (Plain Old Java Object) and does not implement the SessionBean interface.

An EJB 2.x session bean class includes one or more ejbCreate methods, the callback methods ejbActivate, ejbPassivate, ejbRemove, and setSessionContext, and the business methods defined in the local/remote interface. An EJB 3.0 session bean class includes only the business methods.

In EJB 3.0, EJB component interfaces and home interfaces are not required for session beans. A remote interface in an EJB 2.x session EJB extends the javax.ejb.EJBObject interface; a local interface extends the javax.ejb.EJBLocalObject interface. A home interface in an EJB 2.x session EJB extends the javax.ejb.EJBHome interface; a local home interface extends the javax.ejb.EJBLocalHome interface. In EJB 3.0 the home/local home and remote/local interfaces are not required. The EJB interfaces are replaced with a POJI (Plain Old Java Interface) business interface. If a business interface is not included with the session bean class, a POJI business interface gets generated from the session bean class by the EJB server.

An EJB 2.x session EJB includes a deployment descriptor that specifies the EJB name, the bean class name, and the interfaces. The deployment descriptor also specifies the bean type of Stateless/Stateful. In EJB 3.0, a deployment descriptor is not required for a session bean. An example EJB 2.x session bean, which implements the SessionBean interface, is listed next:

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class CatalogBean implements SessionBean {
private SessionContext ctx;
public String getJournal(String publisher) {
if (publisher.equals("Oracle Publisher"))
return new String("Oracle Magazine");
if (publisher.equals("OReilly"))
return new String("dev2dev");
}
public void ejbCreate() {
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
}
}

In EJB 3.0, metadata annotations are used to specify the session bean type and local and remote business interfaces. A stateless session bean is specified with the annotation @Stateless, a stateful session bean with the annotation @Stateful. Component and home interfaces are not required for a session bean. A session bean is required to implement a business interface. The business interface, which is a POJI, may be a local or remote interface. A local interface is denoted with the annotation @Local and a remote interface is denoted with the annotation @Remote. A session bean may implement one or both (local and remote) of the interfaces. If none of the interfaces is specified, a local business interface gets generated. The remote and local business interface class may be specified in the @Local and @Remote annotations. For example, a local business interface may be specified as @Local ({CatalogLocal.class}).

The EJB 3.0 session bean corresponding to the EJB 2.x stateless session bean is annotated with the metadata annotation @Stateless. The EJB 3.0 bean class does not implement the SessionBean interface. The EJB 3.0 session bean implements a business interface. The @Local annotation specifies the local business interface for the session bean. The EJB 3.0 session bean corresponding to the EJB 2.x example session bean is listed next:

import javax.ejb.*;
@Stateless
@Local( { CatalogLocal.class })
public class CatalogBean implements CatalogLocal {
public String getJournal(String publisher) {
if (publisher.equals("Oracle Publisher"))
return new String("Oracle Magazine");
if (publisher.equals("OReilly"))
return new String("java.net");
}
}

In EJB 3.0, the component and home interfaces of EJB 2.x are replaced with a business interface. The business interfaces for the session bean are POJIs, and do not extend the EJBLocalObject or the EJBObject. A local business interface is denoted with the annotation @Local. A remote business interface is denoted with the annotation @Remote. A remote business interface does not throw the RemoteException. The local business interface corresponding to the session bean class is listed next:

import javax.ejb.*;
@Local
public interface CatalogLocal {
public String getJournal(String publisher);
}

A client for an EJB 2.x session bean gets a reference to the session bean with JNDI. The JNDI name for the CatalogBean session bean is CatalogLocalHome. The local/remote object is obtained with the create() method. The client class for the EJB 2.x session bean is listed.

import javax.naming.InitialContext;
public class CatalogBeanClient {
public static void main(String[] argv) {
try {
InitialContext ctx = new InitialContext();
Object objref = ctx.lookup("CatalogLocalHome");
CatalogLocalHome catalogLocalHome = (CatalogLocalHome) objref;
CatalogLocal catalogLocal = (CatalogLocal) catalogLocalHome
.create();
String publisher = "OReilly";
String journal = catalogLocal.getJournal(publisher);
System.out.println("Journal for Publisher: " + publisher + " "
+
journal);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}

In EJB 3.0, a reference to a resource may be obtained with a dependency injection with the @EJB annotation. JNDI lookup and create() method invocation is not required in EJB 3.0. The client class for the EJB 3.0 session bean is listed next:

public class CatalogClient {
@EJB
CatalogBean catalogBean;
String publisher="OReilly";
String journal=catalogBean.getJournal(publisher);
System.out.println("Journal for Publisher: "+publisher +" "+journal);
}