Presentation is loading. Please wait.

Presentation is loading. Please wait.

13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.

Similar presentations


Presentation on theme: "13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs."— Presentation transcript:

1 13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs

2 13-2 Copyright © 2004, Oracle. All rights reserved. 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

3 13-3 Copyright © 2004, Oracle. All rights reserved. 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

4 13-4 Copyright © 2004, Oracle. All rights reserved.

5 13-5 Copyright © 2004, Oracle. All rights reserved. Representing Data in Entity Beans Table Entity bean object int x String s float f int y double d

6 13-6 Copyright © 2004, Oracle. All rights reserved. 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

7 13-7 Copyright © 2004, Oracle. All rights reserved. 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.

8 13-8 Copyright © 2004, Oracle. All rights reserved.

9 13-9 Copyright © 2004, Oracle. All rights reserved. Session Beans Versus Entity Beans 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 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

10 13-10 Copyright © 2004, Oracle. All rights reserved.

11 13-11 Copyright © 2004, Oracle. All rights reserved. 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.

12 13-12 Copyright © 2004, Oracle. All rights reserved. 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

13 13-13 Copyright © 2004, Oracle. All rights reserved.

14 13-14 Copyright © 2004, Oracle. All rights reserved. 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

15 13-15 Copyright © 2004, Oracle. All rights reserved. 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.

16 13-16 Copyright © 2004, Oracle. All rights reserved.

17 13-17 Copyright © 2004, Oracle. All rights reserved. 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)

18 13-18 Copyright © 2004, Oracle. All rights reserved. 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

19 13-19 Copyright © 2004, Oracle. All rights reserved.

20 13-20 Copyright © 2004, Oracle. All rights reserved. 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

21 13-21 Copyright © 2004, Oracle. All rights reserved.

22 13-22 Copyright © 2004, Oracle. All rights reserved. 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.

23 13-23 Copyright © 2004, Oracle. All rights reserved. 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.

24 13-24 Copyright © 2004, Oracle. All rights reserved. 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

25 13-25 Copyright © 2004, Oracle. All rights reserved. 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

26 13-26 Copyright © 2004, Oracle. All rights reserved. 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

27 13-27 Copyright © 2004, Oracle. All rights reserved. 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

28 13-28 Copyright © 2004, Oracle. All rights reserved. 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(); }

29 13-29 Copyright © 2004, Oracle. All rights reserved.

30 13-30 Copyright © 2004, Oracle. All rights reserved. Life Cycle of an Entity Bean Does not exist newInstance() setEntityContext() Pooled unsetEntityContext() Ready ejbLoad() ejbStore() ejbCreate() ejbPostCreate() ejbActivate() ejbRemove() ejbPassivate() Clients invoke business methods

31 13-31 Copyright © 2004, Oracle. All rights reserved.

32 13-32 Copyright © 2004, Oracle. All rights reserved. Deployment Descriptor... False......

33 13-33 Copyright © 2004, Oracle. All rights reserved.

34 13-34 Copyright © 2004, Oracle. All rights reserved. Deployment Descriptor... Public PUBLIC Public methods PUBLIC... *

35 13-35 Copyright © 2004, Oracle. All rights reserved. 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

36 13-36 Copyright © 2004, Oracle. All rights reserved. Practice 13-1: Overview This practice reviews entity bean concepts using paper-based questions.

37 13-37 Copyright © 2004, Oracle. All rights reserved.

38 13-38 Copyright © 2004, Oracle. All rights reserved.


Download ppt "13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs."

Similar presentations


Ads by Google