180 likes | 402 Views
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
E N D
Entity BeansBMP Celsina Bignoli bignolic@smccd.net
Entity Beans • Representpersistent 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 EJB Container 1: create() Client Code Home Object 2:ejbCreate() 6: return EJB object Entity Bean Instance 5: create EJB object 4:return Primary Key EJB Object 3: create database data Database
Destroying an Entity Bean EJB Container Home Object 1: remove() 2:ejbRemove() Client Code Entity Bean Instance EJB Object 1: remove() 2:ejbRemove() 3: remove database data Database
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.ejbEnterpriseBean javax.ejb.EntityBean void setEntityContext(javax.ejb.EntityContext) void unsetEntityContext() void ejbRemove() void ejbActivate() void ejbPassivate() void ejbLoad() void ejbStore()
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 1: newInstance() 2: setEntityContext() 1: unsetEntityContext() 2: JVM will garbage collect and call finalize() pooled ejbHome() ejbFind() 1: ejbActivate() 2: ejbLoad() 1: ejbCreate() 2: ejbPostCreate() 1: ejbStore() 2: ejbPassivate() ejbRemove() ready ejbLoad() ejbStore() Business Method