session 5 tp5

33
ACCP2005/ EJB 2.00/Session 5/ 1of 33 Session 5 Introduction to Entity Beans

Upload: phanleson

Post on 18-Nov-2014

1.265 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Session 5 Tp5

ACCP2005/ EJB 2.00/Session 5/ 1of 33

Session 5

Introduction to Entity Beans

Page 2: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 2 of 33

Session Objectives Describe the concept of

persistence. Define entity beans. List the features offered by entity

beans. Compare entity and session beans. Explain Message-Driven Beans.

Page 3: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 3 of 33

Review of Session 4

In Session 4 we discussed: Definition of a Stateful Session Bean. The characteristics of a Stateful

Session Bean. How to program Stateful session

beans. Difference between Stateless and

Stateful Session beans.

Page 4: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 4 of 33

Persistence

Persistence

Java Object Serialization

Three ways to make an object persistent

Object RelationalMapping

Object database Persistence

DatabaseStorageObj 1

Obj 2

Persistence Objects

Storage

Storage

Page 5: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 5 of 33

Java Object Serialization

Object 1

Object 2

Object n

State of the object

Compact representation

Marshall an object graph into a compact representation

Serialization of object graph into byte streamDeveloper pushes

data over the network or saves the stream to a storage

Byte Stream

Page 6: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 6 of 33

Object Relational Mapping

Travel Account

String NameString TktNoDouble Amount

DatabaseAPI

Relational Database

ManualMapping

Use an Object-Relational

Mapping ProductObject

Page 7: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 7 of 33

Object Database Persistence

Object database persistence is designed to store Java Objects as whole objects which means that there is no need to program a relational database.

The Object Query Language (OQL) provides the facility to query the persisted objects.

This language adds a layer of abstraction from the relational database queries.

The EJB QL eliminates the inconsistencies caused due to the use of the different Query languages used by different application servers.

Page 8: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 8 of 33

Components deployed in Multi-tier Deployment- Application logic components

Application logic components are components that provide methods which perform common tasks

Computing the price of a ticket

Billing of products

Page 9: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 9 of 33

Components deployed in Multi-tier Deployment - Persistent Data Components

Object Serialization of data Database

Examples are air ticket information such as ticket number and amount

Employee data such as salaries and place of work

Page 10: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 10 of 33

Files in an Entity bean

Entity Bean

Remote Interface

Primary Key Class

Entity Bean Class

Deployment Descriptors

Home Interface

Local Interfaces

Page 11: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 11 of 33

Entity Bean classModels

Java Class

PersistentData

Maps

Database Schema

EntityDefinition

For example, an entity bean class can map to a relational table definition. An entity bean instance of that class will then map to a row in that table.

Page 12: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 12 of 33

The Entity Bean’s Remote Interface

ClientEntityBean

InvokesBusinessMethod

signatures

Remote Interface

Page 13: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 13 of 33

The Home Interfacecreate

find

destroy

The home interface is used by the clients to create, find and destroy entity bean objects.

Client

Uses

Methods

HomeInterface

Entity BeanObject

Entity BeanObject

Entity BeanObject

Page 14: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 14 of 33

Local Interface

Local Clients

Entity Beans

Methods

Entity Beans

Exposes

Local Interface allows the beans to expose its methods to other beans that reside within the same container (local clients).

Container

Page 15: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 15 of 33

The Primary Key Class Primary keys make each entity bean look

different. A primary key is an object itself which contains

other objects and data that is necessary to identify an entity bean data instance.

The primary key class has to be serializable, and has to follow the rules of Java object serialization.

The unique identifier, or primary key, enables client to locate the particular entity bean.

Page 16: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 16 of 33

The Deployment Descriptors

Container

Deployment descriptors

List of properties

Contacts the deployment descriptor for bean deployment

Informs the container about the bean and classes

Page 17: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 17 of 33

Pooling of Entity Bean Instances

EJB

Container

EntityBean 1

EntityBean 2

EntityBean n

InstantiatesStorage

Data 1

Data 2

Data n

Represents

Entity Beans

Pooled andRecycled

EntityBean 1

When the bean instance is used ,it is assigned to handle different client

request.

Page 18: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 18 of 33

Ways to Persist Entity Beans

Data Store

EntityBean

Entity Beans mapthemselves in the data store

Bean-managed Persistence

Container-Managed

PersistenceDeploymentDescriptor

Persistent Beans

Deployment Descriptor tells the container about the persistent fields and then the container handles the data logic

Page 19: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 19 of 33

Working with Entity Beans The entity bean instance and the underlying

database can be considered as one and the same. When the bean-managed persistent bean calls the

ejbCreate() method, it creates the database data. In case of container-managed persistence, the

container contains required data access logic, leaving the bean’s methods empty of data access logic.

The finder methods are used to find the existing entity bean in storage. The finder methods do not create any new database data, but they load old entity bean data.

Page 20: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 20 of 33

Modifying Entity Beans Directly though the Database

EJB Container/ Server

EntityBeans

Existing Application

Database

O/R MappingDirect Database Modifications

Bean data

Page 21: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 21 of 33

Developing and Using Entity Beans

In order to write an entity bean class, the javax.ejb.EntityBean interface has to be implemented. It is this interface that defines the call back method used by the container which the bean must implement.

* setEntityContext(javax.ejb.EntityContext)

* unsetEntityContext()

* ejbRemove()

* ejbActivate()

* ejbPassivate()

* ejbLoad()

* ejbStore()

Page 22: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 22 of 33

ejbCreate( ) Used to initialize the fields of a bean instance

which can be used for a particular client to create the underlying database data.

The parameters vary with respect to the ejbCreate() method. Therefore, there are multiple ways to initialize an entity bean instance.

The ejbCreate() methods in the home interface have to be duplicated in the bean class.

Page 23: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 23 of 33

ejbFind ( )

Home object

EJB object

Entity bean instance

EJB Container/ Server

1. Call create()

6. Returns object to the client2. Call ejbCreate()

4. Returns primary key

Client Code

3. Create database data

5. Create EJB object

Creating a BMP EJB and an EJB object

Page 24: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 24 of 33

ejbFind ( ) - rules All finder methods have to begin with “ejbFind”. There has to be at least one finder method by

the name ejbFindByPrimaryKey(). The method finds a unique entity bean instance based on the unique primary key.

There can be various finder methods, with different names and different parameters.

The finder method has to return the primary key for the entity bean it finds. Other than this, it can also give an enumeration of primary keys.

The client will never call the finder methods on the bean instance itself

Page 25: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 25 of 33

ejbRemove( ) - I Called to remove data from the

database The instance of the bean can be

recycled to handle data from a different database

Does not take any parameters

Page 26: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 26 of 33

ejbRemove( )- II

Home object

EJB object

Entity bean instance

EJB Container/ Server

1. Call remove()2. Call ejbRemove()

Client Code

3. Remove database data

1. Call remove()

2. Call ejbRemove()

Destroying an entity bean’s data representation

Page 27: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 27 of 33

Entity Contexts Enterprise beans have a context object which

identifies the bean’s environment. Context objects are accessible to the beans in

order to retrieve information, such as transaction and security information.

The interface to be used for entity beans is the javax.ejb.EntityContext.

Two new methods have been added to entity beans:

* getEJBObject()

* getPrimaryKey()

Page 28: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 28 of 33

Message-Driven Beans Allows JMS applications to receive messages

asynchronously (the sender is independent of the receiver receiving and processing the messages).

Includes business logic, which may include operations such as:

* Performing computation on received data * Initiating a step or condition in a workflow * Storing data * Sending a message

Page 29: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 29 of 33

Uses of Message-Driven Beans Asynchronous messaging. Integrating two applications in a loosely

coupled but reliable manner. Mere message delivery or message

content should drive other events in the system.

Create message selectors which are designed to consume only specific messages and thus act as triggers.

Page 30: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 30 of 33

Implementation of Message Beans It mainly implements two interfaces

javax.ejb.MessageDrivenBean interface and the javax.jms.MessageListener interface.

The developer needs to implement the main business-logic in the onMessage() method. The bean implements three other methods viz. ejbCreate() ejbRemove() and setMessageDrivenContext() which the container uses for managing the lifecycle of the message-driven bean.

Page 31: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 31 of 33

Summary - 1 There are three main ways to make objects

persistent: Java Object Serialization Object-Relational Mapping Object Database Management System

Object serialization is a method by which the current state of Java objects can be captured and saved permanently.

There are two ways of mapping the objects to the relational data:

Manual mapping: This is done using a database access API, which can be JDBC or SQL/J.

Use an object-relational mapping product. It can be Sun’s JavaBlend or Object people’s TOP link.

Page 32: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 32 of 33

Summary -2 Two kinds of components are deployed in a multi-

tier deployment: Application logic components Persistent data components

The entity bean comprises the following files: The entity bean class The remote interface The Local Interface The home interface The primary key class The deployment descriptors

ejbActivate(): When a bean has to be transitioned out of an instance pool, the ejbActivate() callback method is used.

Page 33: Session 5 Tp5

ACCP2005/EJB 2.0/ Session 5 / 33 of 33

Summary -3 ejbPassivate(): When the bean is being sent

into the instance pool, this method is called.

Enterprise beans have a context object that identifies the bean’s environment. These context objects are accessible to the beans in order to retrieve transaction and security information.