session 5 tp5
DESCRIPTION
TRANSCRIPT
ACCP2005/ EJB 2.00/Session 5/ 1of 33
Session 5
Introduction to Entity Beans
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.
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.
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
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
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
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.
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
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
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
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.
ACCP2005/EJB 2.0/ Session 5 / 12 of 33
The Entity Bean’s Remote Interface
ClientEntityBean
InvokesBusinessMethod
signatures
Remote Interface
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
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
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.
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
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.
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
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.
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
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()
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.
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
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
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
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
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()
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
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.
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.
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.
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.
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.