Kĩ thuật lập trình - Java - Ejb: Entity bean
ejbPassivate(): When the bean is being sent into the instance pool, this method is called.
Enterprise beans have a context object that identifies the bean’s environment. These context objects are accessible to the beans in order to retrieve transaction and security information.
34 trang |
Chia sẻ: nguyenlam99 | Lượt xem: 1002 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Kĩ thuật lập trình - Java - Ejb: Entity bean, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
EJB: Entity BeanPresenter: Nguyễn Xuân VinhInformation Technology FacultyNong Lam UniversityACCP2005/EJB 2.0/ Session 5 / * of 33Session ObjectivesDescribe the concept of persistence.Define entity beans.List the features offered by entity beans.Compare entity and session beans.Explain Message-Driven Beans.ACCP2005/EJB 2.0/ Session 5 / * of 33Review of Session 4In Session 4 we discussed: Definition of a Stateful Session Bean.The characteristics of a Stateful Session Bean.How to program Stateful session beans. Difference between Stateless and Stateful Session beans.ACCP2005/EJB 2.0/ Session 5 / * of 33PersistencePersistenceJava Object SerializationThree ways to make an object persistentObject RelationalMappingObject database Persistence DatabaseStorageObj 1Obj 2Persistence ObjectsStorageStorageACCP2005/EJB 2.0/ Session 5 / * of 33Java Object SerializationObject 1Object 2Object nState of the objectCompact representation Marshall an object graph into a compact representationSerialization of object graph into byte streamDeveloper pushes data over the network or saves the stream to a storageByte StreamACCP2005/EJB 2.0/ Session 5 / * of 33Object Relational MappingTravel AccountString NameString TktNoDouble AmountDatabaseAPIRelational DatabaseManualMappingUse an Object-RelationalMapping ProductObject ACCP2005/EJB 2.0/ Session 5 / * of 33Object Database PersistenceObject database persistence is designed to store Java Objects as whole objects which means that there is no need to program a relational database.The Object Query Language (OQL) provides the facility to query the persisted objects.This language adds a layer of abstraction from the relational database queries.The EJB QL eliminates the inconsistencies caused due to the use of the different Query languages used by different application servers. ACCP2005/EJB 2.0/ Session 5 / * of 33Components deployed in Multi-tier Deployment- Application logic components Application logic components are components that provide methods which perform common tasksComputing the price of a ticketBilling of productsACCP2005/EJB 2.0/ Session 5 / * of 33Components deployed in Multi-tier Deployment - Persistent Data ComponentsObjectSerialization of dataDatabaseExamples are air ticket information such as ticket number and amountEmployee data such as salaries and place of workACCP2005/EJB 2.0/ Session 5 / * of 33Files in an Entity bean Entity BeanRemote Interface Primary Key ClassEntity Bean Class Deployment DescriptorsHome InterfaceLocal InterfacesACCP2005/EJB 2.0/ Session 5 / * of 33Entity Bean classModels Java ClassPersistentDataMapsDatabase SchemaEntityDefinitionFor example, an entity bean class can map to a relational table definition. An entity bean instance of that class will then map to a row in that table. ACCP2005/EJB 2.0/ Session 5 / * of 33The Entity Bean’s Remote InterfaceClientEntityBeanInvokesBusinessMethodsignaturesRemote InterfaceACCP2005/EJB 2.0/ Session 5 / * of 33The Home InterfacecreatefinddestroyThe home interface is used by the clients to create, find and destroy entity bean objects.ClientUsesMethods HomeInterfaceEntity BeanObjectEntity BeanObjectEntity BeanObjectACCP2005/EJB 2.0/ Session 5 / * of 33Local InterfaceLocal Clients Entity BeansMethods Entity BeansExposesLocal Interface allows the beans to expose its methods to other beans that reside within the same container (local clients). ContainerACCP2005/EJB 2.0/ Session 5 / * of 33The Primary Key ClassPrimary keys make each entity bean look different.A primary key is an object itself which contains other objects and data that is necessary to identify an entity bean data instance.The primary key class has to be serializable, and has to follow the rules of Java object serialization. The unique identifier, or primary key, enables client to locate the particular entity bean.ACCP2005/EJB 2.0/ Session 5 / * of 33The Deployment DescriptorsContainerDeployment descriptorsList of propertiesContacts the deployment descriptor for bean deployment Informs the container about the bean and classesACCP2005/EJB 2.0/ Session 5 / * of 33Pooling of Entity Bean InstancesEJBContainerEntityBean 1EntityBean 2EntityBean nInstantiatesStorageData 1Data 2Data nRepresentsEntity BeansPooled andRecycledEntityBean 1When the bean instance is used ,it is assigned to handle different client request. ACCP2005/EJB 2.0/ Session 5 / * of 33Ways to Persist Entity BeansData StoreEntityBeanEntity Beans mapthemselves in the data storeBean-managed PersistenceContainer-Managed PersistenceDeploymentDescriptorPersistent BeansDeployment Descriptor tells the container about the persistent fields and then the container handles the data logicACCP2005/EJB 2.0/ Session 5 / * of 33Working with Entity BeansThe entity bean instance and the underlying database can be considered as one and the same.When the bean-managed persistent bean calls the ejbCreate() method, it creates the database data. In case of container-managed persistence, the container contains required data access logic, leaving the bean’s methods empty of data access logic. The finder methods are used to find the existing entity bean in storage. The finder methods do not create any new database data, but they load old entity bean data. ACCP2005/EJB 2.0/ Session 5 / * of 33Modifying Entity Beans Directly though the DatabaseEJB Container/ ServerEntityBeansExisting ApplicationDatabaseO/R MappingDirect Database ModificationsBean dataACCP2005/EJB 2.0/ Session 5 / * of 33Developing and Using Entity Beans In order to write an entity bean class, the javax.ejb.EntityBean interface has to be implemented. It is this interface that defines the call back method used by the container which the bean must implement. * setEntityContext(javax.ejb.EntityContext) * unsetEntityContext() * ejbRemove() * ejbActivate() * ejbPassivate() * ejbLoad() * ejbStore()ACCP2005/EJB 2.0/ Session 5 / * of 33ejbCreate( )Used to initialize the fields of a bean instance which can be used for a particular client to create the underlying database data. The parameters vary with respect to the ejbCreate() method. Therefore, there are multiple ways to initialize an entity bean instance. The ejbCreate() methods in the home interface have to be duplicated in the bean class. ACCP2005/EJB 2.0/ Session 5 / * of 33ejbFind ( ) Home objectEJB objectEntity bean instanceEJB Container/ Server1. Call create()6. Returns object to the client2. Call ejbCreate()4. Returns primary key Client Code3. Create database data5. Create EJB object Creating a BMP EJB and an EJB objectACCP2005/EJB 2.0/ Session 5 / * of 33ejbFind ( ) - rulesAll finder methods have to begin with “ejbFind”. There has to be at least one finder method by the name ejbFindByPrimaryKey(). The method finds a unique entity bean instance based on the unique primary key. There can be various finder methods, with different names and different parameters. The finder method has to return the primary key for the entity bean it finds. Other than this, it can also give an enumeration of primary keys.The client will never call the finder methods on the bean instance itself ACCP2005/EJB 2.0/ Session 5 / * of 33ejbRemove( ) - ICalled to remove data from the databaseThe instance of the bean can be recycled to handle data from a different databaseDoes not take any parameters ACCP2005/EJB 2.0/ Session 5 / * of 33ejbRemove( )- II Home objectEJB objectEntity bean instanceEJB Container/ Server1. Call remove()2. Call ejbRemove()Client Code3. Remove database data1. Call remove()2. Call ejbRemove()Destroying an entity bean’s data representation ACCP2005/EJB 2.0/ Session 5 / * of 33Entity ContextsEnterprise beans have a context object which identifies the bean’s environment. Context objects are accessible to the beans in order to retrieve information, such as transaction and security information. The interface to be used for entity beans is the javax.ejb.EntityContext. Two new methods have been added to entity beans: * getEJBObject() * getPrimaryKey()ACCP2005/EJB 2.0/ Session 5 / * of 33Message-Driven BeansAllows JMS applications to receive messages asynchronously (the sender is independent of the receiver receiving and processing the messages).Includes business logic, which may include operations such as: * Performing computation on received data * Initiating a step or condition in a workflow * Storing data * Sending a messageACCP2005/EJB 2.0/ Session 5 / * of 33Uses of Message-Driven BeansAsynchronous messaging.Integrating two applications in a loosely coupled but reliable manner. Mere message delivery or message content should drive other events in the system. Create message selectors which are designed to consume only specific messages and thus act as triggers. ACCP2005/EJB 2.0/ Session 5 / * of 33Implementation of Message BeansIt mainly implements two interfaces javax.ejb.MessageDrivenBean interface and the javax.jms.MessageListener interface. The developer needs to implement the main business-logic in the onMessage() method. The bean implements three other methods viz. ejbCreate() ejbRemove() and setMessageDrivenContext() which the container uses for managing the lifecycle of the message-driven bean. ACCP2005/EJB 2.0/ Session 5 / * of 33Summary - 1There are three main ways to make objects persistent:Java Object SerializationObject-Relational MappingObject Database Management SystemObject serialization is a method by which the current state of Java objects can be captured and saved permanently. There are two ways of mapping the objects to the relational data:Manual mapping: This is done using a database access API, which can be JDBC or SQL/J.Use an object-relational mapping product. It can be Sun’s JavaBlend or Object people’s TOP link.ACCP2005/EJB 2.0/ Session 5 / * of 33Summary -2 Two kinds of components are deployed in a multi-tier deployment:Application logic componentsPersistent data componentsThe entity bean comprises the following files:The entity bean classThe remote interfaceThe Local InterfaceThe home interfaceThe primary key classThe deployment descriptorsejbActivate(): When a bean has to be transitioned out of an instance pool, the ejbActivate() callback method is used.ACCP2005/EJB 2.0/ Session 5 / * of 33Summary -3ejbPassivate(): When the bean is being sent into the instance pool, this method is called. Enterprise beans have a context object that identifies the bean’s environment. These context objects are accessible to the beans in order to retrieve transaction and security information. HỎI ĐÁP
Các file đính kèm theo tài liệu này:
- ch15_ejbentitybean_0515.ppt