Entity Beans()

cavalcadejewelΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 4 μήνες)

66 εμφανίσεις

Entity Beans

BMP

Celsina Bignoli

bignolic@smccd.net

Entity Beans


Represent

persistent objects

saved to
permanent storage


bank account, employee data, etc…


lifetime is completely independent from the
user session


have an identity, used to pass references
between applications and share entities with
other clients

Entity Beans (2)


advantages


compact representation


can associate methods


can use implicit middleware services from an
application server


can be cached for performance

Persistence


Object
-
relational mapping


save an object to a relational database table


use JDBC to map the object data into a
relational database


read data from the database and load it into the
fields of a object instance


more sophisticated than Java serialization


can take advantage of SQL query capabilities


mapping can be handcrafted or facilitated using
a object
-
relational mapping tool like Oracle
TopLink or Hibernate



Entity Bean Features


survive failures


they are just representation of data in a fault
-
tolerant
storage


have a lifetime much longer than a client session


they are a view into a database


they should not be seen as a separate version of the
data in the database


the EJB container maintains the in memory data and the
data in the database in sync using the
ejbLoad
() and
ejbStore
() methods implemented by the entity bean
class

Entity Bean Features(2)


multiple instances represent same data


one instance servicing multiple requests would require
multi
-
threading but this would lead to complex, error
-
prone implementations


one instance servicing one request at a time would
create bottlenecks


several in
-
cache instances represent same underlying
database data must solve possible
data corruption


container must synchronize instances periodically using
ejbLoad() and ejbStore() methods


the frequency of the synchronization is dictated by
transactions

Entity Bean Features(3)


instances can be pooled


entity bean objects are recyclable


the container may pool and reuse instances to
represent different entries in the underlying data
storage


more efficient


must acquire and release resources no longer
in use using ejbActivate() and ejbPassivate()
methods


ejbPassivate entails a state save (ejbSave())


ejbActivate entails a state load (ejbLoad())

How to Persist Entity Beans


bean
-
managed persistence


developer must write code to load, save and
find data from data storage


must use persistence API such as JDBC


container
-
managed persistence


strip the bean from any persistence logic


inform the container how you’d like the data to
be persisted using container tools


container generates
automatically

the
persistence code

Creating an Entity Bean

Entity Bean

Instance

Home Object

EJB Object

Client Code

Database

3: create database data

2:ejbCreate()

4:return Primary Key

1: create()

5: create EJB object

6: return EJB

object

EJB Container

Destroying an Entity Bean

Entity Bean

Instance

Home Object

EJB Object

Client Code

Database

3: remove database data

2:ejbRemove()

EJB Container

2:ejbRemove()

1: remove()

1: remove()

Finding Entity Beans


since entity beans are persisted to data
storage they can be found (unlike session
beans, which only live for the duration of a
client session)


home interface exposes
finder methods

to
find entity beans


session beans
do not

have finder methods

Bean
-
managed Persistent (BMP) Entity
Beans


developer
must

write code to map the entity
bean to and from storage


Can use:



a database API such as JDBC


a object/relation mapping framework (TopLink
or Hibernate)


usually, preferred to Container
-
managed
persistent (CMP) entity beans ONLY when
performance is an issue

BMP Entity Bean


Coding Basics


must implement the
javax.ejb.EntityBean

interface



javax.ejb.EntityBean


void setEntityContext(javax.ejb.EntityContext)

void unsetEntityContext()

void ejbRemove()

void ejbActivate()

void ejbPassivate()

void ejbLoad()

void ejbStore()

javax.ejbEnterpriseBean

BMP Entity Bean


Coding Basics (2)


must define
finder methods

ejbFind<…>()
in the local and remote home interfaces


must implement
finder methods

in the
bean implementation to find an existing
entity bean in storage


Must define
at least

one finder method
ejbFindByPrimaryKey()



Entity Bean Files


remote/local interface


the enterprise bean class


maps to an entity in a database (table). An instance
maps to a row in a table


exposes callback methods used by the container to
manage the bean


exposes methods to update fields


primary key class


uniquely identifies each bean


must be serializable


deployment descriptor

BMP Life cycle

does not exist

pooled

ready

1: newInstance()

2: setEntityContext()

1: unsetEntityContext()

2: JVM will garbage collect

and call finalize()

ejbHome()

ejbFind()

ejbLoad()

ejbStore()

1: ejbActivate()

2: ejbLoad()

1: ejbStore()

2: ejbPassivate()

Business Method

1: ejbCreate()

2: ejbPostCreate()

ejbRemove()