ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

H N I V N Â U X N Ễ Y U G N

:

V G

EJB: Entity Bean

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

6 1

/

1 0

/

5 1

X X

/

1

Presenter: Nguy n Xuân Vinh Information Technology Faculty Nong Lam University

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Session Objectives

H N I V N Â U X N Ễ Y U G N

:

V G

2   G N Ạ M H N Ì R T P Ậ L

Describe the concept of persistence. Define entity beans. List the features offered by entity beans. Compare entity and session beans. Explain Message­Driven Beans.

:

N Ô M

6 1

/

1 0

/

5 1

X X

/

2

ACCP2005/EJB 2.0/ Session 5 / 2 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Review of Session 4

H N I V N Â U X N Ễ Y U G N

:

V G

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.

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

6 1

/

1 0

/

5 1

X X

/

3

ACCP2005/EJB 2.0/ Session 5 / 3 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Persistence

H N I V N Â U X N Ễ Y U G N

:

V G

Storage

Java Object Serialization

Persistence    Objects

Database

Obj 1

Storage

Object database Persistence

2   G N Ạ M H N Ì R T P Ậ L

:

Persistence

N Ô M

Obj 2

Storage

6 1

/

Object Relational Mapping

1 0

/

5 1

Three ways to make an object persistent

X X

/

4

ACCP2005/EJB 2.0/ Session 5 / 4 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Java Object Serialization

H N I V N Â U X N Ễ Y U G N

:

V G

Marshall an object graph into a compact representation

Object 1

Compact representation

State of the object

Object 2

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

Serialization of object graph into byte stream

Object n

6 1

/

1 0

/

Developer pushes data over the network or saves the stream to a storage

5 1

Byte Stream

X X

/

5

ACCP2005/EJB 2.0/ Session 5 / 5 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Object Relational Mapping

H N I V N Â U X N Ễ Y U G N

:

V G

Travel Account

Manual Mapping

Database API

String Name String TktNo Double Amount

Use an Object- Relational Mapping Product

2   G N Ạ M H N Ì R T P Ậ L

Object

:

N Ô M

6 1

/

Relational Database

1 0

/

5 1

X X

/

6

ACCP2005/EJB 2.0/ Session 5 / 6 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Object Database Persistence

H N I V N Â U X N Ễ Y U G N

:

V G

 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.

2   G N Ạ M H N Ì R T P Ậ L

:

 This  language  adds  a  layer  of  abstraction  from  the

N Ô M

relational database queries.

6 1

/

1 0

/

5 1

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

X X

/

7

ACCP2005/EJB 2.0/ Session 5 / 7 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Components deployed in Multi-tier Deployment- Application logic components

H N I V N Â U X N Ễ Y U G N

:

V G

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

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

6 1

/

1 0

/

5 1

Billing of products

Computing the price of a ticket

X X

/

8

ACCP2005/EJB 2.0/ Session 5 / 8 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Components deployed in Multi-tier Deployment - Persistent Data Components

H N I V N Â U X N Ễ Y U G N

:

V G

Object

Database

Serialization of data

2   G N Ạ M H N Ì R T P Ậ L

:

Employee data such as salaries and place of work

N Ô M

Examples are air ticket information such as ticket number and amount

6 1

/

1 0

/

5 1

X X

/

9

ACCP2005/EJB 2.0/ Session 5 / 9 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Files in an Entity bean

H N I V N Â U X N Ễ Y U G N

:

V G

Entity Bean Class

Remote Interface

2   G N Ạ M H N Ì R T P Ậ L

:

Entity Bean

Deployment Descriptors

N Ô M

6 1

/

1 0

/

Home Interface

5 1

X X

/

Primary Key Class

Local Interfaces

0 1

ACCP2005/EJB 2.0/ Session 5 / 10 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Entity Bean class

H N I V N Â U X N Ễ Y U G N

:

Maps

V G

Models

Persistent Data

Java Class

2   G N Ạ M H N Ì R T P Ậ L

:

Database Schema

N Ô M

6 1

/

1 0

Entity Definition

/

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.

5 1

X X

/

1 1

ACCP2005/EJB 2.0/ Session 5 / 11 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

The Entity Bean’s Remote Interface

H N I V N Â U X N Ễ Y U G N

:

V G

Client

Entity Bean

2   G N Ạ M H N Ì R T P Ậ L

:

Remote Invokes Interface Business Method signatures

N Ô M

6 1

/

1 0

/

5 1

X X

/

2 1

ACCP2005/EJB 2.0/ Session 5 / 12 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

The Home Interface

H N I V N Â U X N Ễ Y U G N

:

create

V G

Entity Bean Object

Home Interface

Uses

find

Client

Entity Bean Object

Methods

2   G N Ạ M H N Ì R T P Ậ L

:

destroy

N Ô M

Entity Bean Object

6 1

/

1 0

/

5 1

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

X X

/

3 1

ACCP2005/EJB 2.0/ Session 5 / 13 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Local Interface

H N I V N Â U X N Ễ Y U G N

:

V G

Local Clients

Entity Beans

Exposes

2   G N Ạ M H N Ì R T P Ậ L

Entity Beans

:

N Ô M

Methods

6 1

/

1 0

/

Container

5 1

X X

/

4 1

Local Interface allows the beans to expose its methods to other beans that reside within the same container (local clients). ACCP2005/EJB 2.0/ Session 5 / 14 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

The Primary Key Class

H N I V N Â U X N Ễ Y U G N

:

V G

 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

2   G N Ạ M H N Ì R T P Ậ L

:

to follow the rules of Java object serialization.

N Ô M

 The unique identifier, or primary key, enables client to

locate the particular entity bean.

6 1

/

1 0

/

5 1

X X

/

5 1

ACCP2005/EJB 2.0/ Session 5 / 15 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

The Deployment Descriptors

H N I V N Â U X N Ễ Y U G N

:

V G

Contacts the deployment descriptor for bean deployment

Container

Deployment descriptors

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

List of properties

6 1

/

Informs the container about the bean and classes

1 0

/

5 1

X X

/

6 1

ACCP2005/EJB 2.0/ Session 5 / 16 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Pooling of Entity Bean Instances

H N I V N Â U X N Ễ Y U G N

:

V G

Represents

Instantiates

Storage

EJB

Entity Entity Bean 1 Bean 1

Data 1

Container

Data 2

Entity Bean 2

Data n

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

Entity Bean n

Entity Beans

6 1

/

1 0

/

5 1

Pooled and Recycled

X X

/

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

7 1

ACCP2005/EJB 2.0/ Session 5 / 17 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Ways to Persist Entity Beans

H N I V N Â U X N Ễ Y U G N

:

V G

Bean-managed Persistence

Entity Bean

Entity Beans map themselves in the data store

:

Data Store

2   G N Ạ M H N Ì R T P Deployment Descriptor tells the Ậ L container about the persistent fields N Ô and then the container handles the data logic M

6 1

/

1 0

/

5 1

Deployment Descriptor

Container- Managed Persistence

X X

/

8 1

ACCP2005/EJB 2.0/ Session 5 / 18 of 33

Persistent Beans

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Working with Entity Beans

 The entity bean instance and the underlying database can be

considered as one and the same.

H N I V N Â U X N Ễ Y U G N

:

 When the bean­managed persistent bean calls the ejbCreate()

V G

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

2   G N Ạ M H N Ì R T P Ậ L

:

storage. The finder methods do not create any new database data,  but they load old entity bean data.

N Ô M

6 1

/

1 0

/

5 1

X X

/

9 1

ACCP2005/EJB 2.0/ Session 5 / 19 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Modifying Entity Beans Directly though the Database

H N I V N Â U X N Ễ Y U G N

:

V G

EJB Container/ Server

Entity Beans Existing Application

2   G N Ạ M H N Ì R T P Ậ L

O/R Mapping

:

N Ô M

Direct Database Modifications

Bean data

6 1

/

1 0

/

5 1

X X

/

0 2

ACCP2005/EJB 2.0/ Session 5 / 20 of 33

Database

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Developing and Using Entity Beans

H N I V N Â U X N Ễ Y U G N

:

V G

 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)

2   G N Ạ M H N Ì R T P Ậ L

* unsetEntityContext()

:

* ejbRemove()

N Ô M

* ejbActivate()

6 1

* ejbPassivate()

/

1 0

/

5 1

* ejbLoad()

X X

/

* ejbStore()

1 2

ACCP2005/EJB 2.0/ Session 5 / 21 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

ejbCreate( )

H N I V N Â U X N Ễ Y U G N

:

V G

 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.

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

6 1

/

1 0

/

5 1

X X

/

2 2

ACCP2005/EJB 2.0/ Session 5 / 22 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

ejbFind ( )

H N I V N Â U X N Ễ Y U G N

:

EJB Container/ Server

V G

Client Code

1. Call create() 2. Call ejbCreate() Home object

Entity bean instance

6. Returns object to the client

5. Create EJB object 4. Returns primary key

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

EJB object

3. Create database data

6 1

/

1 0

/

5 1

Creating a BMP EJB and an EJB object

X X

/

3 2

ACCP2005/EJB 2.0/ Session 5 / 23 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

ejbFind ( ) - rules

 All finder methods have to begin with  “ejbFind”.   There has to be at least one finder method by the name

H N I V N Â U X N Ễ Y U G N

:

V G

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.

2   G N Ạ M H N Ì R T P Ậ L

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

:

itself

N Ô M

6 1

/

1 0

/

5 1

X X

/

4 2

ACCP2005/EJB 2.0/ Session 5 / 24 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

ejbRemove( ) - I

 Called to remove data from the database  The instance of the bean can be recycled to handle data from a

H N I V N Â U X N Ễ Y U G N

:

different database

V G

 Does not take any parameters

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

6 1

/

1 0

/

5 1

X X

/

5 2

ACCP2005/EJB 2.0/ Session 5 / 25 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

ejbRemove( )- II

H N I V N Â U X N Ễ Y U G N

:

V G

EJB Container/ Server

Entity bean instance

2. Call ejbRemove() 1. Call remove() Home object Client Code

2. Call ejbRemove()

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

EJB object 1. Call remove()

3. Remove database data

6 1

/

1 0

/

5 1

Destroying an entity bean’s data representation

X X

/

6 2

ACCP2005/EJB 2.0/ Session 5 / 26 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Entity Contexts

 Enterprise beans have a context object which identifies the bean’s

environment.

H N I V N Â U X N Ễ Y U G N

:

V G

 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.

2   G N Ạ M H N Ì R T P Ậ L

:

 Two new methods have been added to entity beans:           * getEJBObject()                    *  getPrimaryKey()

N Ô M

6 1

/

1 0

/

5 1

X X

/

7 2

ACCP2005/EJB 2.0/ Session 5 / 27 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Message-Driven Beans

H N I V N Â U X N Ễ Y U G N

:

 Allows JMS applications to receive messages asynchronously (the  sender is independent of the receiver receiving and processing the  messages).

V G

2   G N Ạ M H N Ì R T P Ậ L

:

 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

N Ô M

6 1

/

1 0

/

5 1

X X

/

8 2

ACCP2005/EJB 2.0/ Session 5 / 28 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Uses of Message-Driven Beans

H N I V N Â U X N Ễ Y U G N

Asynchronous messaging. Integrating two applications in a loosely coupled but

:

V G

reliable manner.

Mere message delivery or message content should drive

other events in the system.

Create message selectors which are designed to consume

2   G N Ạ M H N Ì R T P Ậ L

:

only specific messages and thus act as triggers.

N Ô M

6 1

/

1 0

/

5 1

X X

/

9 2

ACCP2005/EJB 2.0/ Session 5 / 29 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Implementation of Message Beans

 It mainly implements two interfaces

H N I V N Â U X N Ễ Y U G N

:

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

V G

 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.

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

6 1

/

1 0

/

5 1

X X

/

0 3

ACCP2005/EJB 2.0/ Session 5 / 30 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Summary - 1

 There are three main ways to make objects persistent:

H N I V N Â U X N Ễ Y U G N

:

 Java Object Serialization  Object­Relational Mapping  Object Database Management System

V G

 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

2   G N Ạ M H N Ì R T P Ậ L

:

people’s TOP link.

N Ô M

6 1

/

1 0

/

5 1

X X

/

1 3

ACCP2005/EJB 2.0/ Session 5 / 31 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Summary -2

 Two kinds of components are deployed in a multi­tier

H N I V N Â U X N Ễ Y U G N

:

deployment:  Application logic components  Persistent data components

V G

 The entity bean comprises the following files:

2   G N Ạ M H N Ì R T P Ậ L

:

 The entity bean class  The remote interface  The Local Interface  The home interface  The primary key class  The deployment descriptors

N Ô M

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

6 1

/

1 0

/

5 1

X X

/

2 3

ACCP2005/EJB 2.0/ Session 5 / 32 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

Summary -3

 ejbPassivate():  When the bean is being sent into the instance

pool, this method is called.

H N I V N Â U X N Ễ Y U G N

:

V G

 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.

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

6 1

/

1 0

/

5 1

X X

/

3 3

ACCP2005/EJB 2.0/ Session 5 / 33 of 33

ƯỜ Ạ Ọ Ệ TR NG Đ I H C NÔNG LÂM TP.HCM KHOA CÔNG NGH  THÔNG TIN

H I ĐÁP

H N I V N Â U X N Ễ Y U G N

:

V G

2   G N Ạ M H N Ì R T P Ậ L

:

N Ô M

6 1

/

1 0

/

5 1

X X

/

4 3