260 likes | 276 Views
Learn about features, components, and usage scenarios of entity beans in Java EE development. Explore creating, finding, managing, and selecting entity beans in the business tier.
E N D
Managing Persistent Data in the Business Tier Entity EJBs
Objectives • After completing this lesson, you should be able to do the following: • Identify the features of an entity bean • Distinguish between session beans and entity beans • Decide when to use an entity bean • Describe the various components of an entity bean • Differentiate bean-managed persistent beans from container-managed persistent beans
Entity Beans • Are objects that can be stored in permanent storage • Represent persistent data in the database • Can be uniquely identified • Do not contain complex business logic • Do not model business processes but manage data for such processes • Can serve multiple clients simultaneously
int x String s float f int y double d Representing Data in Entity Beans Entity bean object Table
When to Use Entity Beans • You can use an entity bean in the following situations: • To represent a business entity • When the state of the bean must be persistent • When you need to represent a relationship between entities
Callback Methods to Load and Store Data • ejbLoad()loads the data from the persistent storage to the bean. • ejbStore()saves the data from the bean instance to the persistent storage.
Session Beans Versus Entity Beans • Entity Beans: • Are represented by nouns because they model business data • Are long-lived and do not depend on a client’s session • Contain callback methods for managing data (create, retrieve, persist, and so on) in the persistent storage • Session Beans: • Are represented by verbs because they model workflow • Are short-lived and have a lifetime of a client session • Manage their state, but do not contain callback methods to manage data because they do not represent data in the persistent storage
Types of Entity Beans • Container-managed persistent (CMP) beans: The container provides the logic to search and manipulate the persistent data. • Bean-managed persistent (BMP) beans: The bean provider codes the logic to search and manipulate the persistent data in the callback methods.
BMP Beans Versus CMP Beans • BMP beans: • Contain code for managing data persistence • Provide flexibility for bean developers to manage state • Are complicated to program • CMP beans: • Do not contain code for managing persistence because the container manages the persistence • Use abstract persistence schema and define the data retrieval/manipulation logic in the deployment descriptor • Are easier to program and contain lesser code
Components of an Entity Bean • Home interface: Is used by clients to create, find, and destroy EJB objects • Component interface: Contains the declaration of all business methods in the bean class that can be invoked by the client • Bean class: Represents persistent data and contains methods to access or manipulate that data • Primary key class: Is used to uniquely identify an entity bean instance • Deployment descriptor: Contains information that is used by the container
Creating, Removing, Finding, and Selecting Entity Beans • ejbCreate(): Initializes the entity bean in memory. This method may also insert the corresponding data into the persistent storage. • ejbRemove(): Removes the database data but does not remove the in-memory entity bean instance. The instance may return to the pool and release all resources in the ejbPassivate() method. • Finder methods: Used to find an entity bean. • Selector methods: Used to select entity beans and values of CMP fields.
Home Interface of an Entity Bean • Remote home interface: • Extends the javax.ejb.EJBHome interface • Can be accessed by remote clients by using Java Naming and Directory Interface (JNDI) • Allows client to create handles for later reference • Local home interface: • Extends the javax.ejb.EJBLocalHome interface • Can be accessed by local clients • Remote/local home interface: • Contains methods to create, find, or remove entity objects • Contains business methods that are not applicable to a specific instance (home methods)
Creating a Bean Instance • An entity bean can have one or more create() methods, or none defined in its home interface. • The create() method: • Enables a client to create a row in the database table that corresponds to the entity bean • Contains parameters that initialize the state of the created entity object • Returns the remote/local interface reference of the entity beans • Throws CreateException and any user-defined exceptions • Throws RemoteException if it is part of a remote home interface
Finding an Entity Bean Instance • An entity bean: • Can have one or more finder methods in home interface • Must have a findByPrimaryKey(primarykey) method • Has finder methods that: • Find row or rows in the database table • Contain parameters that locate the requested entity object • Return entity bean’s component interface reference or collection of objects of component interface type • Must throw FinderException • Must throw RemoteException in a remote home interface • Have find as a prefix in their names
Removing an Entity Bean • An entity bean with remote home interface: • Can have one or more remove() methods: remove(Handle handle) and remove(Object primarykey) • Throws RemoteException and RemoveException from remove() methods • An entity bean with local home interface: • Can have one remove() method: remove(Object primarykey) • Throws RemoveException from the remove() method • remove() methods remove the entity object and the row from the underlying database.
Home Methods of Entity Beans • Home methods are provided by a bean provider. • Home methods contain business logic that is not specific to any bean instance. • An entity bean can have one or more home methods, or none. • The arguments and return types of remote home methods should be of RMI-IIOP type.
Component Interfaces of an Entity Bean • Component interfaces define: • Business methods that are accessible by clients • Accessor methods for the bean attributes • Remote interfaces: • Are referenced by remote clients • Extend the javax.ejb.EJBObject interface • Local interfaces: • Are referenced by local clients • Extend the javax.ejb.EJBLocalObject interface
Primary Key Class of an Entity Bean • Primary key: • Uniquely identifies each bean instance • Is used to find or remove an entity bean • Can be of any legal value type in RMI-IIOP • The primary key class: • Should implement java.io.Serializable • Can have a single primary key (a single field) to identify the entity bean • Can have composite keys (multiple fields) to identify the entity bean
Bean Class of an Entity Bean • Initializes the bean instance through the ejbCreate() and ejbPostCreate() methods • Implements: • Finder methods through ejbFindxxx() methods • Home methods through ejbHomexxx() methods • Callback methods from the EntityBean interface • Business and private methods
Bean Class of an Entity Bean • Contains ejbCreate() and ejbPostCreate() methods for each create() method. • The ejbCreate() method: • Is invoked when a client invokes the create() method to initialize persistent fields • Has primary key as return type • The ejbPostCreate() method: • Is invoked after the entity bean is created and before any other request from a client is processed • Has void as return type • Initializes any relationship fields for an entity bean
javax.ejb.EntityBean Interface • All entity bean classes should implement the javax.ejb.EntityBean interface. public interface javax.ejb.EntityBean implements javax.ejb.EnterpriseBean { public void ejbActivate(); public void ejbLoad(); public void ejbPassivate(); public void ejbRemove(); public void ejbStore(); public void setEntityContext(EntityContext ctx); public void unSetEntityContext(); }
Pooled Life Cycle of an Entity Bean Does not exist newInstance() setEntityContext() unsetEntityContext() ejbCreate()ejbPostCreate() ejbRemove() ejbPassivate() ejbActivate() ejbLoad() Ready ejbStore() Clients invoke business methods
Deployment Descriptor <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE …> <ejb-jar> <enterprise-beans> <entity> <ejb-name>...</ejb-name> <home>...</home> <remote>...</remote> <ejb-class>...</ejb-class> <persistence-type>...</persistence-type> <prim-key-class>...</prim-key-class> <reentrant>False</reentrant> <abstract-schema-name>...</abstract-schema-name> <cmp-field> <field-name>...</field-name> </cmp-field> <primkey-field>...</primkey-field> </entity> </enterprise-beans> ...
Deployment Descriptor ... <assembly-descriptor> <security-role> <description>Public</description> <role-name>PUBLIC</role-name> </security-role> <method-permission> <description>Public methods</description> <role-name>PUBLIC</role-name> <method> <ejb-name>...</ejb-name> <method-name>*</method-name> </method> </method-permission> </assembly-descriptor> </ejb-jar>
Summary • In this lesson, you should have learned how to: • Identify features of an entity bean • Use entity beans • Distinguish between a session bean and an entity bean • Develop components of an entity bean • Differentiate BMP beans from CMP beans • Describe the life cycle of an entity bean
Practice 13-1: Overview • This practice reviews entity bean concepts using paper-based questions.