session 7 tp7

30
ACCP2005 /EJB 2.0 /Session 7/1 of 30 Container-Managed Persistent Entity Bean Session 7

Upload: phanleson

Post on 16-Jan-2015

815 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/1 of 30

Container-Managed Persistent Entity Bean

Session 7

Page 2: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/2 of 30

Session Objectives

Define a container-managed Persistent entity bean (CMP EJB)

Define a primary key class

Recognize the implementation guidelines needed to deploy a CMP EJB

Create and deploy multiple CMP EJBs

Describe EJB Query Language

Page 3: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/3 of 30

Review All the BMPs have to implement the

javax.ejb.EntityBean interface. The setEntityContext() method is called when the

container has to increase the pool size. The unsetEntityContext() method dissociates the

bean from its environment. It is called just before the instance of the entity bean is destroyed.

The bean-managed state fields are persistent fields, which load and store data in the database.

The ejb-jar.xml file and the jaws.xml files are packaged into a directory called META-INF.

The bean instance can service any number of finder methods while in the pool.

Page 4: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/4 of 30

Container-managed Fields

The container manages all the data access logic Database operations are executed behind the

scenes Every field that has to be managed should follow

the rules for java object serialization In a CMP, the container can persist every field Features of Container-managed fields are:

EJB Container does the job of persistence Container provides tools that generate code in the EJB

class that maps methods in the bean to a result set. Advanced features like connection pooling and caching

become easy. High level of reuse.

Page 5: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/5 of 30

Container-Managed Relationships (CMR)

EJB 2.0 specifications support persistent relationships between enterprise beans in the same container

These relationships may be one-to-one, one-to-many or many-to-many

Each of the relationships can be uni-directional or bi-directional

Relationships are maintained as abstract bean fields that are accessed by public abstract get and set accessor methods in the enterprise bean class

An entity bean must provide a local client view which is used as the target for the CMR

Page 6: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/6 of 30

Primary Key Class

Has to be a serializable class, similar to the bean-managed persistent entity bean.

The fields present in the primary key class have to come from the container-managed fields of the entity bean class.

Page 7: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/7 of 30

IntroductionImplementing CMP EJB’s

All the CMP EJBs have to implement the javax.ejb.EntityBean interface

The callback methods invoked on the bean by the container are defined by this interface.

Container‑managed Persistent Beans do not require the developer to provide data access logic.

Container provides the implementation to map the bean instances from and to data storage.

Page 8: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/8 of 30

Implementation Guidelines for CMP EJB (1)

Method Explanation Implementation

setEntityContext() This method associates the bean with context information. The bean will be able to access information about its environment.

Requests for resources needed by the instance are to be made. The bean will be in a pool and will not have any specific data in the database.

ejbFind<…>(<…>) The container handles the finding of data. This is why finder methods are not written for CMPs.

There is no need to implement this method for CMP.

ejbCreate(<…>) This method is responsible for creating new data in the database and initializing the fields of the bean instance.

Database data should not be created in this method. It is used to check the client parameters.

Page 9: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/9 of 30

Implementation Guidelines for CMP EJB (2)

Method Explanation Implementation

ejbPostCreate(<…>) For every ejbCreate(), there always has to be an ejbPostCreate. This method is called after every ejbCreate() method.

When the instance of the bean has been associated with an EJB object, the container calls this method. This method is also used in order to reset some transaction-related parameters. For instance, a flag could be used in the bean to indicate if a field has been changed.

ejbActivate() This method is used when the container wants to call a bean from the pool and get it ready.

Bean-specific resources have to be acquired in this method.

Page 10: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/10 of 30

Implementation Guidelines for CMP EJB (3)

Method Explanation Implementation

ejbLoad() This method is called to load the database data into the instance of the bean.

Data from the database is read before this method is called. All the utilities that are needed to work with the data are performed here.

ejbStore() This method is called to update the database with new values of the in-memory fields.

The EJB container will update the database automatically. The container–managed fields are prepared to be written to the database.

Page 11: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/11 of 30

Implementation Guidelines for CMP EJB (4)

Method Explanation Implementation

ejbPassivate() When the entity bean has to return the bean to the pool, this method is called.

All resources have to be released before the ejbPassivate () method is called.

ejbRemove() This method is called to destroy the database data.

Perform all necessary functions before calling this method.

unsetEntityContext() This method will dissociate the bean from the environment.

Resources allocated should be released.

Page 12: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/12 of 30

Remote Interface

Implements the business methods of the entity bean

Extends the javax.ejb.EJBObject as all remote interfaces do

Throws RemoteException

Extends EJBObject

Page 13: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/13 of 30

Local Interface

Provides a separation between the entity bean class (defined by the bean developer) and its persistent representation

Extends from EJBLocalObject as opposed to the remote interface which extends from EJBObject

Contains the business methods of the bean

Page 14: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/14 of 30

Remote Home Interface

Remote Home Interface

create()find()

Database

New Row

CreateException

success

Creates new row in the database

failure

FinderException

failure

success Returns new instanceOr Enumeration

Used by Client

RemoteException

Page 15: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/15 of 30

LocalHome Interface

This interface is implemented by the EJB container and extends EJBLocalHome

The implemented object is called the local home object and serves as a factory for EJB local objects

Contains create(), find(), and remove() methods which call the corresponding ejbCreate(), ejbFind(), and ejbRemove() methods of the bean.

Page 16: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/16 of 30

Primary Key Class

Implements Serializable interface Must be serializable for container-

managed persistence

import java.io.Serializable;/** * The primary key class for the Item Container-Managed Entity * Bean*/public class ItemPK implements java.io.Serializable{...

}

Page 17: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/17 of 30

CMP Bean Class

Extends the EntityBean class

Container-Managed fields are all public.

Implements the business methods of the bean

Implements the EJB-Required methods such as ejbCreate, ejbRemove

Page 18: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/18 of 30

Client Class

Client Class

Getter/Setter methods

NamingContextFactory

Remote Interface

Initial Context

create()find()

lookup()

Property 1

Property 2 Property 3

Page 19: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/19 of 30

The Deployment Descriptors-ejb-jar.xml file (1)

Lists details of the Bean class name Type and name of the primary key Home Interface name Remote Interface Persistence type CMP fields Query method names

Page 20: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/20 of 30

The jaws.xml file

JBoss uses jaws.xml file to access the database

When the bean is deployed, JAWS looks for the jaws.xml file and on locating it, reads it and configures the names of the database tables.

Lists the CMP field’s name, type and the database table-name

Page 21: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/21 of 30

The JBoss.xml descriptor

Provides information about the JNDI mapping names.

This name is used by the client to perform a JNDI lookup and the container to locate the particular bean.

Page 22: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/22 of 30

Creating a jar file

Bean class

Home interface class

Remote interface class META-

INFJAR FILE+

The jar file has to be put into the deploy directory on the server.

Page 23: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/23 of 30

Facts about CMP EJBs

The container takes care of all system-level activities like performing data access logic

CMP reduces the size of the code as the developer can declare the logic declaratively

It is difficult for the developer to figure out the operations being performed behind the scenes by the container.

Page 24: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/24 of 30

The EJB Query Language (EJB QL)

The new EJB 2.0 specifications introduced a standard query language for declaring the behavior of find and select methods.

EJB QL resembles the standard Structured Query language (SQL) but it has been adapted to work with the abstract persistence schema of EJB 2.0.

Is optimizable and portable across databases and data schemas since it can be compiled to a target language, and the execution of queries is shifted to the native language facilities provided by the persistent store instead of executing them on the runtime representation of the entity beans state.

The EJB QL relies on the cmp-fields and the cmr-fields of the related entity beans.

Page 25: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/25 of 30

The Two Query Methods (1)

There are mainly two types of query methods: find() methods and select() methods.

Find methods are used by client applications or beans for locating remote and local object references to specific entity beans.

A simple find() query can be shown as:

public interface CustomerHomeLocal extends javax.ejb.EJBLocalHome{

public CustomerLocal findByPrimaryKey(Integer primmaryKey)throws javax.ejb.FinderException; public Collection findByProductId() throws javax.ejb.FinderException; }

Page 26: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/26 of 30

Query for a find() method

The EJB Query for the sample findByProductId() method is as shown below.

<query> <query-method> <method-name>findByProductId</method-name> <method-params/> <query-method> <ejb-ql> SELECT OBJECT(c) FROM Customer c WHERE c.hasProductId = TRUE </ejb-ql></query>

Page 27: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/27 of 30

The Two Query Methods (2)

The select() method is used to select entity objects or other values derived from an entity bean’s abstract schema type.

A simple select() query can be shown as:

public class AddressBean implements javax.ejb.EntityBean{

public abstract String ejbSelectAreaCode() throws FinderException; }

Page 28: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/28 of 30

Query for a select() method The EJB Query for the sample

ejbSelectAreaCode() method is as shown below.

<query> <query-method> <method-name>ejbSelectAreaCode</method-name> <method-params> <method-param>java.lang.String<method-param> </method-params> <query-method> <ejb-ql> SELECT a.areacode FROM Address AS a WHERE a.areacode.STATE = 1 </ejb-ql></query>

Page 29: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/29 of 30

Summary (1)

A container-managed persistent entity bean will let the container handle all the data access logic.

The container in a CMP can persist every field and it does this behind the scenes.

The fields present in the primary key class have to come from the container-managed fields of the entity bean. This will allow the container to set and extract the entity bean’s primary key fields.

In a container-managed persistent entity bean, the container manages the persistent operations.

The home interface of the bean defines the create() method. This is defined so that a new item can be created in the database.

The container-managed fields of the entity bean class have the primary key fields.

Page 30: Session 7 Tp7

ACCP2005 /EJB 2.0 /Session 7/30 of 30

Summary (2)

When the bean is deployed, JAWS will look for the jaws.xml file and on locating it, will read and configure the names of the database tables.

A jar file is created to package the three Java files, namely, the remote interface, the bean class, and the home interface. The jar file will build an archive, which will contain the three classes and the XML files. The XML files are present in the META-INF directory.

In CMP, the container performs persistence, and so it is very difficult for the developer to know the operations being performed. The container has to be decompiled, and only then will the code be available.

CMP entity beans help to develop persistent code, which results in rapid development of the application.