Entity Beans BMP Celsina Bignoli [email protected].
-
Upload
stewart-nichols -
Category
Documents
-
view
214 -
download
0
Transcript of Entity Beans BMP Celsina Bignoli [email protected].
![Page 2: Entity Beans BMP Celsina Bignoli bignolic@smccd.net.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e665503460f94b60647/html5/thumbnails/16.jpg)
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()