Brewing entity
Enterprise JavaBeans - JavaWorld September
2000
Tutorial Details:
Brewing entity Enterprise JavaBeans
Brewing entity Enterprise JavaBeans
By: By Sandip H. Mandera
Harness the power of entity beans
nterprise JavaBeans (EJB) represents a fundamental change in the world of enterprise software. The EJB specification provides the software development community with the component model, maintains interoperability between EJB and non-EJB applications (including non-Java-based applications like CORBA), and features straightforward programming. The power of EJB resides in the fact that it saves a developer the pain of developing a homegrown infrastructure.
This article will focus on the design and development of container-managed entity beans. I will begin with a brief summary of the changes introduced in EJB Specification 1.1 and follow it with a brief tour of session beans before I delve fully into a discussion of entity beans (the fun part).
Improved recipe
EJB Specification 1.1 tightened some of the issues that were loosely defined by Sun Microsystems in version 1.0. These are the changes/modifications, as defined by EJB Specification 1.1:
Mandatory entity-bean support for container providers.
Improved support for EJB environment management, which means the bean provider must specify the bean's entire environment using entries in a JNDI context.
Clear lines of separation between the roles and responsibilities of the bean provider and that of the application assembler.
Removed support of deployment descriptor features that described the deployer's output. Also, the deployment descriptor is specified in XML.
JDK 1.2 caused the EJB container vendors to change their runtime libraries with the Java class java.security.Identity to java.security.Principal interface.
The package name of the javax.jts interface has changed to javax.transaction . Also, modifications were made in the thrown exceptions.
All finder methods must throw the FinderException .
ejbCreate() requires a different value. Instead of void() , ejbCreate() returns the primary key class reference.
As a component developer, you must understand what each bean flavor provides its clients. EJB Specification 1.1 supports both session and entity beans. Before we brew the beans, let's taste each flavor.
Taste session beans
Before tasting the session beans, we must first understand their main ingredient: their sessions. A session represents a conversation instance. And like any conversation, a session exists for a limited period of time. It ends when the thread connecting the conversation dies.
A session bean is a logical extension of the conversation instance, but resides on the server. For its entire life, a session bean exists in the container, which manages security, concurrency, etc. Also, the client view of a session bean does not depend upon location. A session bean presents the same set of APIs to its client, which can be another EJB, a Java applet, a Java application, a Java servlet, or a CORBA client. Whether the client runs in the same Java VM as the session bean is immaterial.
Unlike entity beans (which I will discuss later), session beans do not advertise their identities. Thus, session beans appear to be anonymous to the client. Because of this, when EJBObject.getPrimaryKey() and EjbHome.remove(Object primaryKey) methods are invoked on session beans, you obtain java.rmi.RemoteException . This means component developers should not provide finder methods when writing session beans.
Also, single-client applications (I use the word application loosely) use session beans, which are relatively short-lived and unable to survive an application crash or server restart.
Now that we have tasted session beans, let's savor the power of entity beans.
Taste entity beans
An entity bean represents a component view of the data residing in a persistent storage area, like a database or a flat file. You would usually map a row in a database to an entity bean. For instance, let's say the following employee table resides in a database with the following schema:
Employee_ID:SMALLINT
Employee_Name:VARCHAR(50)
Employee_Address:VARCHAR(50)
The component developer would create an entity bean called EmployeeBean that contained the three attributes present in the employee table. Like session beans, entity beans are server-side components and live within a container. However, unlike session beans, entity beans can endure a server restart or container crash. Also, the container provides various services like security, concurrency, transaction management, and persistence -- in the case of container-managed entity beans.
Different clients can concurrently access each entity bean. The container manages the underlying synchronization of the entity beans' data in a container-managed persistence. In a bean-managed persistence, you must write code to ensure consistent data management. Like session beans, every entity bean offers the same view to its client, irrespective of whether the client runs in the same Java VM. Also like session beans, an entity bean's client can be another bean, a Java applet, a Java application, Java servlet, or even a CORBA client.
A container can manage multiple entity beans deployed within it, and provides a home interface for each deployed bean. An entity bean's home interface provides the bean's client with a set of APIs to create , find , and remove entity bean instances. To create entity bean components, you must implement the following two interfaces over and above the component (i.e. the bean):
The home interface: EJBHome
The remote interface: EJBRemote
Home interface
The entity bean developer creates a home interface, which inherits the javax.ejb.EJBHome interface. Let's look at javax.ejb.EJBHome and the methods an entity bean would have in its home interface:
public interface EJBHome extends Remote
{
void remove(Handle someHandle) throws RemoteException, RemoveException;
void remove(Object aPrimaryKey) throws RemoteException, RemoveException;
}
The methods an entity bean would have on its home interface are:
Create methods
Unlike session beans, the EJB specification allows for overloaded create() methods to create entity beans. Typically, you would pass data as arguments to this method; you could then initialize the entity bean with the needed state when it is created. The specification mandates that the return type of the create() method return a reference to the EJBRemote object. Also, the throws clause of every create() method should have java.rmi.RemoteException and javax.ejb.CreateException . However, you have the flexibility to add any application-specific exceptions to the throws clause.
Finder methods
An entity bean's home interface defines one or more finder methods. EJB Specification 1.1 tells you to provide the findByPrimaryKey() method for entity beans. This method allows a client to look up entity beans by their primary key. The name findByPrimaryKey() must not change, and it should provide one argument that represents the entity bean's primary key type. The return type of findByPrimaryKey() must be an entity bean's remote interface. Each finder method should throw java.rmi.RemoteException and javax.ejb.FinderException . If you want to provide your own custom finder methods, the specification allows for other methods in addition to findByPrimaryKey() .
Remove methods
The EJBHome interface provides several ways for a client application to remove an entity bean object that a client is interacting with. A client attempting to use an entity bean that has been removed would get java.rmi.NoSuchObjectException .
Now that we have covered some basics, let's look at the primary key and object identity of an entity bean.
The component developer creates a primary key class for each entity bean. Hence, if two entity objects have the same home interface and primary key, they are considered identical. The EJB specification does not mention object equality based on the = = operator. Also, if you compare two object references using the Java API, Object.equals(Object foo) , the result is unspecified. The only way to compare object equality is through the isIdentical(EJBObject) API.
According to the EJB Specification 1.1, a primary key class is any class with a legal value type in RMI-IIOP. Generally, a primary key class would be associated with a particular entity bean class. However, you are free to use the same primary key class for multiple entity beans.
Now let's see how the employee entity bean home interface and its corresponding PrimaryKey class might look.
////////////////////////////////////////////////////////////////////
// Employee Entity Bean Home Interface
////////////////////////////////////////////////////////////////////
package com.mandera.entitybeansarticle;
import javax.ejb.EJBHome;
import java.ejb.CreateException;
import java.ejb.FinderException;
import java.ejb.RemoteException;
public interface EmployeeHome extends EJBHome
{
public Employee create(int employeeID, String name, String address)
throws CreateException, RemoteException;
public Employee findByPrimaryKey(EmployeePrimaryKey empKey)
throws FinderException, RemoteException;
}
////////////////////////////////////////////////////////////////////
// Primary Key Class
////////////////////////////////////////////////////////////////////
package com.mandera.entitybeansarticle;
public class EmployeePrimaryKey implements java.io.Serializable
{
public int employeeId;
}
The EJB specification requires that the instance of PrimaryKey be serializable.
The remote interface
The entity bean's client accesses the entity bean object through its remote interface, which provides the client with a set of business methods. An entity bean's remote interface must extend the java.ejb.EJBObject interface.
The following example illustrates the use of remote interface:
////////////////////////////////////////////////////////////////////
// Employee Entity Bean Remote Interface
//////////////////
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Brewing entity
Enterprise JavaBeans - JavaWorld September
2000
View Tutorial: Brewing entity
Enterprise JavaBeans - JavaWorld September
2000
Related
Tutorials:
Accelerate EJB 2.0
development with EJBGen
Accelerate EJB 2.0
development with EJBGen |
Integrate EJBs with CORBA
Integrate EJBs with CORBA |
Container-managed relations for the
21st century
Container-managed relations for the
21st century |
Rumble in the
jungle: J2EE versus .Net, Part
1
Rumble in the
jungle: J2EE versus .Net, Part
1 |
Sun boosts
Sun boosts enterprise Java |
Make the Java-Oracle9i
connection
Make the Java-Oracle9i
connection |
The J2EE 1.4 Tutorial
The J2EE 1.4 Tutorial is a guide to developing enterprise applications for the Java 2 Platform, Enterprise Edition (J2EE) version 1.4. Here we cover all the things you need to know to make the best use of this tutorial. |
Finally, getting hands in !
Finally, getting hands in ! |
Turn EJB components into Web services
Summary
Web services have become the de facto standard for communication among applications. J2EE 1.4 allows stateless Enterprise JavaBeans (EJB) components to be exposed as Web services via a JAX-RPC (Java API for XML Remote Procedure Call) endpoint, al |
Developing Your First EJBs, Part 2
the authors walked through what you need to do to develop your first entity bean. This week concludes this series with a look at how to develop a session bean, building on the examples presented in part one.
|
The power of table-oriented programming
The power of table-oriented programming
When object-oriented programming languages began to be used in enterprise applications, designers had problems fitting the object-oriented model with the relational model. In the object-oriented model, data is enca |
A lightweight nonintrusive EJB testing framework
A lightweight nonintrusive EJB testing framework
Summary
This article presents a simple, easy-to-deploy framework that enables fine-grained tests to be run on the container, making it possible to develop and maintain Enterprise JavaBeans components usin |
An Introduction to Java Object Persistence with EJB
The 'impedance mismatch' between relational databases' tabular orientation and object-oriented Java's hierarchical one is a perennial problem for which the Java world has several good solution offerings. This article, the first in a three-part series, wil |
Distributed Enterprise Messaging with MantaRay
A very important communication standard in Java is Java Messaging Service (JMS). JMS is a set of Java interfaces and associated semantics that define a way for a Java-based client to access a messaging system. JMS provides a rich, yet simple, set of messa |
JSP (JavaServer Pages) is a standard for combining Java and HTML to provide dynamic content in web pages.
With JSP, you embed Java code in HTML using special JSP tags similar to HTML tags. You install the JSP page, which has a .jsp extension, into the WebLogic Server document root, just as you would a static HTML page. When WebLogic Server serves a JSP page.. |
Testing Your Enterprise JavaBeans with Cactus
Enterprise JavaBeans provide many advantages. But each server-side/back-end developer knows that development of EJBs is sometimes painful, time-consuming, and requires a lot of patience while creating assembly descriptors, application-server-specific conf |
Developing Distributed application using Enterprise Java Beans, J2EE Architecture, EJB Tutorial, WebLogic Tutorial.
Developing Distributed application using Enterprise Java Beans, J2EE Architecture, EJB Tutorial, WebLogic Tutorial.
Distributed Architecture
Two-tier application:
In the past two-tier applications were used. Two-tier applications are also know as |
Introduction To Enterprise Java Bean(EJB). WebLogic 6.0 Tutorial.
Introduction To Enterprise Java Bean(EJB). WebLogic 6.0 Tutorial.
Welcome to EJB Section
(Learn to Develop World Class Applications with Enterprise Java Beans)
(Online WebLogic 6.0 Tutorial)
Introduction To Enterprise Java Bean(EJB)
Enterprise |
developing a Session Bean and a Servlet and deploy the web application on
JBoss 3.0
developing a Session Bean and a Servlet and deploy the web application on JBoss 3.0
Writing Stateless Session Bean and Calling through Servlet
Previous Tutorial Index Next
In this lesson I will show you how to develop a Stateless Session Bean and |
NetBeans IDE 4.1
Out-of-the-box support for J2EE 1.4 and Web Services. Check out what early access release 2 can do for you! |
|
|
|