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

Environmental dependencies and JNDI Access


An enterprise bean's context may be divided into 3 components:

  • Container context

  • Resources

  • Environment context

The container may be used to supply references to resources and environment entries. Environmental dependencies and JNDI access may be encapsulated with dependency annotations, a dependency injection mechanism, and a simple lookup mechanism. Dependency injection implies that the EJB container automatically supplies/injects a bean's variable or setter method with a reference to a resource or environment entry in the bean's context. Alternatively, you would have to use the javax.ejb.EJBContext or JNDI APIs to access the environment entries and resources. Dependency injection is implemented by annotating a bean's variable or setter method with one of the following annotations:

  • @javax.ejb.EJB is used to specify dependency on another EJB.

  • @javax.annotation.Resource is used to specify dependency on an external resource such as a JDBC datasource, a JMS destination, or a JMS connection factory. The @Resource annotation is not specific to EJB 3, and may be also used with other Java EE components.

For accessing multiple resources, use the corresponding grouping annotations @javax.ejb.EJBs and @javax.annotation.Resources. An example of injecting dependency on an EJB into a bean's variable using the @javax.ejb.EJB annotation is as follows:

import javax.ejb.EJB;
@Stateful
public class CatalogBean implements Catalog {
@EJB(beanName = "HelloBean")
private Hello hello;
public void helloFromCatalogBean() {
hello.hello();
}
}

In the preceding example, the hello variable is injected with the EJB HelloBean. The type of the hello variable is Hello, which is the HelloBean's business interface that it implements. Subsequently, we invoked the hello() method of the HelloBean. A resource may also be injected into a setter method. If the resource type can be determined from the parameter type, the resource type is not required to be specified in the @Resource annotation. In the following code snippet, the setter method is annotated with the @Resource annotation. In the setter method, the dataSource property is set to a JNDI resource of type javax.sql.DataSource with value as catalogDB.

private javax.sql.DataSource dataSource;
@Resource(name="catalogDB")
public void setDataSource (DataSource jndiResource) {
this.dataSource = jndiResource;
}

The setter method must follow the JavaBean conventions: the method name begins with set, returns void, and has only one parameter. If the name of the resource is the same as the property name, the resource name is not required to be specified in the @Resource annotation. The JNDI name of the resource is of the format class_name/catalogDB, class_name being the class name.

private javax.sql.DataSource catalogDB;
@Resource
public void setCatalogDB (DataSource jndiResource) {
this.catalogDB = jndiResource;
}

Setter injection methods are invoked by the container before any business methods on the bean instance. Multiple resources may be injected using the @Resources annotation. For example, in the following code snippet two resources of type javax.sql.DataSource are injected.

@Resources({
@Resource(name="ds1", type="javax.sql.DataSource"),
@Resource(name="ds2", type="javax.sql.DataSource")
})

JNDI resources injected with the dependency mechanism may be looked up in the java:comp/env namespace. For example, if the JNDI name of a resource of type javax.sql.DataSource is catalogDB, the resource may be looked up as follows.

InitialContext ctx = new InitialContext();
Javax.sql.DataSource ds = ctx.lookup("java:comp/env/catalogDB");