Entity Beans BMP Celsina Bignoli [email protected].

16
Entity Beans BMP Celsina Bignoli [email protected]

Transcript of Entity Beans BMP Celsina Bignoli [email protected].

Page 1: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

Entity BeansBMP

Celsina [email protected]

Page 2: 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

Page 3: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

Entity Beans (2)

• advantages– compact representation– can associate methods– can use implicit middleware services from an

application server– can be cached for performance

Page 4: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 5: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 6: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 7: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 8: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 9: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

Creating an Entity Bean

Entity BeanInstance

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

Page 10: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

Destroying an Entity Bean

Entity BeanInstance

Home Object

EJB Object

Client Code

Database

3: remove database data

2:ejbRemove()

EJB Container

2:ejbRemove()

1: remove()

1: remove()

Page 11: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 12: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 13: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 14: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 15: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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

Page 16: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.

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