Files in an Entity bean Entity Bean Remote Interface Primary Key Class Entity Bean Class Deployment Descriptors Home Interface Local Interfaces... Persistent DataMaps Database Schema
Trang 1EJB: Entity Bean
Presenter: Nguyễn Xuân VinhInformation Technology Faculty
Trang 3 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
Trang 4Three ways to make an object persistent
Object Relational Mapping
Object database Persistence
Database Storage
Trang 6Object Relational Mapping
Relational Database
Manual Mapping
Use an Relational Mapping Product
Object-Object
Trang 7Object 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
Trang 8TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCMComponents deployed in Multi-tier Deployment- KHOA CÔNG NGHỆ THÔNG TIN
Application logic components
Application logic components are components that provide methods which
perform common tasks
Computing the price
of a ticket
Billing of products
Trang 9TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCMComponents deployed in Multi-tier DeploymentKHOA CÔNG NGHỆ THÔNG TIN -
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
Trang 10Files in an Entity bean
Entity Bean
Remote Interface
Primary Key Class
Entity Bean Class
Deployment Descriptors
Home Interface Local
Interfaces
Trang 11Persistent Data
Maps
Database Schema
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
Trang 12The Entity Bean’s Remote Interface
Client
Entity Bean
Invokes
Business Method signatures Remote Interface
Trang 13Entity Bean Object
Entity Bean Object
Entity Bean Object
Trang 14Entity Beans
Exposes
Local Interface allows the beans to expose its methods to other beans that
Container
Trang 15The 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
Trang 16The Deployment Descriptors
Container
Deployment descriptors
List of properties
Contacts the deployment descriptor for bean deployment
Informs the container about the bean and classes
Trang 17Entity Bean 2
Entity Bean n
Instantiates
Storage Data 1 Data 2 Data n Represents
Entity Beans
Pooled and Recycled
Entity Bean 1
When the bean instance
is used ,it is assigned to handle different client
Trang 18Ways to Persist Entity Beans
Data Store
Entity Bean
Entity Beans map themselves in the data store
Bean-managed Persistence
Managed Persistence
Container-Deployment Descriptor
Persistent Beans
Deployment Descriptor tells the
container about the persistent fields
and then the container handles the data logic
Trang 19Working 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
Trang 20Existing Application
Database
Modifications
Bean data
Trang 21Developing 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
Trang 22 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
Trang 23ejbFind ( )
Home object
EJB object
Entity bean instance
EJB Container/ Server
1 Call create()
6 Returns object to the client
2 Call ejbCreate()
4 Returns primary key
Client Code
3 Create database data
5 Create EJB object
Creating a BMP EJB and an EJB object
Trang 24 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
Trang 25 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
Trang 26ejbRemove( )- II
Home object
EJB object
Entity bean instance
EJB Container/ Server
Client Code
3 Remove database data
1 Call remove()
2 Call ejbRemove()
Destroying an entity bean’s data representation
Trang 27 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()
Trang 28 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
Trang 30Implementation 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
Trang 31 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.
Trang 32 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
instance pool, the ejbActivate() callback method is used.
Trang 33pool, 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