Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

An Introduction to Java Object Persistence with EJB : Page 4

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, will look specifically at the EJB specification and its unique way of solving the object-relational mapping problem.


advertisement
Figure 2. Web to EJB: The figure shows how a client request travels from the client tier to the Application tier. Once the controller servlet receives the request, it converts it to a business service request and invokes the appropriate business service in the Business Service tier. The business service uses one or more entity beans to load data from and save data to resources in the Data tier.
The EJB Deployment and Runtime Environment
We will use JBoss 3.0.2 as our deployment and runtime environment for the examples that follow. We will design a simple Web application that allows user accounts to be created and retrieved using a Web browser communicating with a Java servlet, which communicates with a user service, which communicates with an entity EJB (see Figure 2).

Writing and Deploying an Entity Bean
The following four steps illustrate the typical process to develop and deploy an entity bean:
  1. Write the classes and interfaces for your entity bean
  2. Write a deployment descriptor
  3. Package the entity bean and associated files inside of a jar file
  4. Deploy the bean.
An entity bean is composed of a minimum of three classes/interfaces:
  1. The Component Interface—Because we are only concerned about accessing our EJB from the same JVM, we will create an interface that extends javax.ejb.EJBLocalObject.
  2. package com.jeffhanson.datatier.ejb; import javax.ejb.EJBLocalObject; public interface LocalUser extends EJBLocalObject { public String getUserID(); //primary key public String getFullName(); public String setAddress(String address); public String getAddress(); public String setCity(String city); public String getCity(); public String setState(String state); public String getState(); public String setZip(String zip); public String getZip(); }

  3. The Home Interface—Again, because we are only concerned about accessing our EJB from the same JVM, we will create an interface that extends javax.ejb.EJBLocalHome.
  4. package com.jeffhanson.datatier.ejb; import javax.ejb.CreateException; import javax.ejb.FinderException; import javax.ejb.EJBLocalHome; import java.util.Collection; public interface LocalUserHome extends EJBLocalHome { public LocalUser create(String userID, String fullName, String address, String city, String state, String zip) throws CreateException; public Collection findByFullName(String fullName) throws FinderException; public LocalUser findByPrimaryKey(String userID) throws FinderException; }

  5. The Enterprise Bean Class—The final class implements either javax.ejb.SessionBean or javax.ejb.EntityBean (see Listing 1).
The Deployment Descriptor
To deploy an EJB into an EJB container's environment, you must supply a deployment descriptor file to the EJB container. A deployment descriptor file is an XML document, named ejb-jar.xml, that specifies information about the bean such as its persistence type and transaction attributes. You need to package the Java classes and the deployment descriptor into a JAR or EAR file.

In addition to the standard ejb-jar.xml file, JBoss has an additional deployment descriptor for declaring the CMP persistence properties for enterprise beans, named jaws.xml. Listing 2 illustrates the ejb-jar.xml deployment descriptor for UserEJB.

Notice that Listing 2 specifies the type and name of the primary key for the EJB and the fields of the EJB that are persistent. A table is created with a name that is the same as that of the Bean class, which in this case will be named 'UserEJB'. Listing 3 illustrates the jaws.xml deployment descriptor for UserEJB.

Notice in Listing 3 that each field is declared within a cmp-field element. This will instruct the EJB container to create a column for each field in the new database table and map each field to its particular column.

All you need to do to deploy UserEJB in JBoss is create a jar file with the compiled classes for the EJB and the deployment descriptors for the EJB and place it into the deploy directory of your particular JBoss server.

The EJB Client
The architecture of this application makes it possible for you to access the EJB from within the same JVM. This design simplifies a few things, specifically the type of casting needed in order to obtain the home interface for the EJB. Listing 4 illustrates the UserService object, which will access the EJB.

Notice that the UserService in Listing 4 first attempts to find an instance of the EJB by calling the findByPrimaryKey method and passing a user ID. If the instance is not found, the UserService calls the create method on the EJB's home instance in order to create the EJB. The findByPrimaryKey method is intercepted by the EJB container, which attempts to retrieve a record from the database with a primary key named by the user ID. The create method will add a new record to the database with a primary key named by the user ID.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap