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

Metadata annotations


Metadata annotations were introduced in JDK 5.0 as a means to provide data about an application. Annotations are used for the following purposes:

  • Generating boilerplate code (code that is repeated in different sections of a Java program) automatically.

  • Replacing configuration information in configuration files such as deployment descriptors.

  • Replacing comments in a program.

  • Informing the compiler about detecting errors and generating or suppressing warnings. The @Deprecated annotation is used to inform the compiler about a deprecated feature, on detecting which the compiler generates a warning. The @Override annotation informs the compiler about an overridden element. If the element is not overridden properly, the compiler generates an error. The @SuppressWarnings annotation is used to inform the compiler to suppress specific warnings.

  • Runtime processing of annotations by annotating the annotations with the @Retention(RetentionPolicy.RUNTIME) annotation.

EJB 3.0 specification has introduced some metadata annotations for annotating EJB 3.0 applications. EJB 3.0 metadata annotations have reduced the number of classes and interfaces a developer is required to implement. Also, the metadata annotations have eliminated the requirement for an EJB deployment descriptor. Three types of metadata annotations are used in EJB 3.0: EJB 3.0 annotations, object/relational mapping annotations, and annotations for resource injection and security. Though annotations follow a different semantic than Java code, they help in reducing code lines and—in the case of EJB—increase cross-platform portability. The EJB 3.0 annotations are defined in the javax.ejb package. For example, the @Stateless annotation specifies that an EJB is a Stateless Session Bean:

import javax.ejb.Stateless;
@Stateless
public class HelloBean implements Hello {
public void hello() {
System.out.println("Hello EJB 3.0!");
}
}

For all the new EJB 3.0, annotations, refer to the EJB 3.0 specification document EJBCore (ejb-3_0-fr-spec-ejbcore.pdf). Persistence annotations are defined in the javax.ejb.persistence package. For example, the @Entity annotation specifies that the EJB is an Entity Bean:

import javax.persistence.*;
@Entity
@Table(name = "Catalog")
public class Catalog implements Serializable {
private long id;
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}

The resource injection and security annotations are defined in the Common Annotations for the Java Platform specification, and are in the javax.annotation and javax.annotation.security packages. For example, the @Resource injection may be used to inject a javax.sql.DataSource resource. First, configure a data source in a Java EE container. Subsequently, inject a data source handle by annotating a declaration for a variable of type javax.sql.DataSource with the @Resource annotation.

@Resource
private javax.sql.DataSource mysqlDS;
public getCatalogEntry(){
Connection conn = mysqlDS.getConnection();
}

Data source injection using the @Resource annotation precludes the requirement for JNDI lookup using an InitialContext object. The security annotations are presented in the following table.

Annotation

Description

DeclareRoles

Declares references to security roles

RolesAllowed

Declares the methods that are allowed to invoke the methods of the entity bean

PermitAll

Specifies that all security roles are allowed to invoke the specified methods.

DenyAll

Specifies that no security roles are allowed to invoke the specified methods.

RunAs

Specify a security role as the bean's run-as property.