1 / 16

Entity Beans BMP

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

stella
Télécharger la présentation

Entity Beans BMP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Entity BeansBMP Celsina Bignoli bignolic@smccd.net

  2. 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

  3. Entity Beans (2) • advantages • compact representation • can associate methods • can use implicit middleware services from an application server • can be cached for performance

  4. 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

  5. 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

  6. 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

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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

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

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

  15. 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

  16. 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

More Related