Title: Entity
1Entity Bean
2What is an Entity Bean
? Persistent Data components ? Application
logic components
3Files included in Entity Bean
? Home ? Remote ? Bean ? Primary Key ?
Deployment Descriptor
4Features of Entity Bean
? Long lived ? Survive Failures ? Their
instance are a view into a Database In memory
object and the database itself as really being
one and the same. In memory entity bean is
simply a view or lens into the database through
ejbLoad() and ejbStore() Not called when the
business methods are executed.
5Features of Entity Bean (Contd)
? Several Entity bean instances may represent
the same underlying data EJB dictates that only
a single thread can ever be running within a bean
instance. Reasons are If we would like an
instance of an entity bean to serve many client
we need to write a thread safe program because
all client will access the bean with a
thread. Having a multiple threads running in a
EJB Transaction Management is impossible.
6Features of Entity Bean (Contd)
Since only one thread can work at a time in a
bean, other threads must wait until their turn
comes. This would grind performance into
halt. Hence container allows multiple instance of
the same entity bean to be created. This will
lead to new problem data corruption. We are
dealing with multiple in memory, which access the
same data. In this case container calls the
ejbLoad() and ejbStore() depending upon the
transaction set to the bean and they give the
client illusion that they have exclusive access
to data when in fact there are many clients all
touching the same data.
7Features of Entity Bean (Contd)
? Entity bean instances can be pooled As a
client connects and disconnects, container
creates and destroys beans as necessary to serve
those clients. This creation of and destruction
of objects is very expensive, especially if
client requests come frequently. Entity bean
instances are recycled object and may be pooled
depending on your containers policy. The
container may pool and reuse entity bean instance
to represent different instances of the same type
to data in underling storage. When your entity
bean is assigned to a particular EJB object, it
may be holding resources such as socket
connections. But when it is in a pool it may not
need that socket. Thus, to allow the bean to
release and acquire resources, your entity bean
class must implement to callback methods
8Features of Entity Bean (Contd)
ejbActivate() is the callback that your
container will invoke on your bean instance when
transitioning your bean out of a generic instance
pool. ejbPassivate() is the callback that your
container will invoke when transitioning your
bean into a generic instance pool. When an entity
bean instance is passivated, it must not only
release held resources but also save its state to
underlying storage-that way, the storage is
updated to the latest entity bean instance state.
To save the instances fields to the database,
the container invokes the entity bean ejbStore()
method prior to ejbPassivate() similarly
ejbActivate() is called prior to ejbStore().
9Entity Bean can be created, removed, or found.
? ejbCreate() To initialize a session bean,
the container calls ejbCreate() allowing the
bean to prepare itself for use. ?
ejbRemove() The container calls the beans
ejbRemove() method, allowing the bean to prepare
itself for destruction.
10Entity Bean can be created, removed, or found
Since entity bean data is uniquely identified in
an underlying storage, entity beans can also be
found rather than created. When you find an
entity bean, youre searching a persistent store
for some entity bean data. This differs from
session bean because session beans cannot be
found they are not permanent objects, and they
live and die with the clients session.
11Finding Existing entity beans ejbFind()
Your entity bean class finder methods are used
to find an existing entity bean in storage.
Finder methods do not create any new database
data- they simply load some old entity bean data.
12Rules about the finder methods
? All finder methods must begin with ejbFind ?
You must have at east one finder method, called
ejbFindByPrimaryKey ? You can have many different
finder methods, with different names and
different parameters to each method. ? A finder
method must return either the primary key for the
entity bean if finds or an enumeration of primary
keys if it finds more than one. ? As ejbCreate(),
clients do not invoke your finder methods on the
bean instance itself.
13Entity Bean can be modified without going through
EJB
By modifying the underlying database where the
bean data is stored you can modify the contents
of EJB
14Two ways to persist Entity Bean
? Bean managed Persistent User need to hard
code the mechanism of storing and retrieving of
data from the database. Like insert, select etc.
? Container managed Persistent In this case
you can strip your bean of any persistence logic.
Rather than hard-coding persistence into your
bean class, you simply tell your EJB container
which public fields of your bean are persistent
fields. You do this via deployment descriptor.
15Interface API
Serializable
Enterprise Bean
Handle
Home Handle
Entity Bean
Session Bean
16EnterpriseBean Interface
public interface EnterpriseBean extends
java.io.Serializable
17EntityBean Interface
public interface javax.ejb.EntityBean extends
javax.ejb.EnterpriseBean public void
ejbActivate() public void ejbLoad()
public void ejbPassivate() public void
ejbRemove() public void ejbStore()
public void setEntityContext(javax.ejb.EntityCont
ext) public void unsetEntityContext()
18SessionBean Interface
public interface javax.ejb.SessionBean extends
javax.ejb.EnterpriseBean public void
ejbActivate() public void ejbPassivate()
public void ejbRemove() public void
setSessionContext(javax.ejb.SessionContext)
19Handle Interface
An EJB object handle is persistent reference to
an EJB object. Handles allow you to disconnect
from your EJB server, shut your application down,
and later resume your application while
preserving the conversational state in the beans
you've been working with. Home handles are also
useful when your client code needs to store a
reference to an EJB object tin stable storage and
re-connect to that EJB object later. public
interface Handle extends java.io.Serializable
public EJBObject getEJBObject() throws
java.rmi.RemoteException
20HomeHandle Interface
Home handles are useful when your client code
needs to store a reference to a home object in
stable storage and re-connect to that home object
later. public interface HomeHandle extends
java.io.Serializable public EJBHome
getEJBHome() throws java.rmi.RemoteException
21Entity Context
All beans have a context object that identifies
the environment of the bean. These context
objects contain environment information that the
EJB container sets. Your beans can access the
context to retrieve all sorts of information,
such as transaction and security information.
22EJBContext
public interface EJBContext public abstract
java.ejb.EJBHome getEJBHome() public abstract
java.util.Properties getEnvironment() public
abstract java.security.Identity
getCallerIdentity() public abstract boolean
isCallerInRole( java.security.Identity) public
abstract javax.jts.UserTransaction
getUserTransaction() public abstract void
setRollbackOnly() public abstract boolean
getRollbackOnly()
23Life Cycle of Entity Bean
24 Presented By Lovin
Varghese, Technical Manager, Software Technology
Group Ltd. Email id lovin_v_at_yahoo.com