Container-Managed Persistence Example


This CMP example illustrates how much work the EJB container does for the entity bean. A CMP implementation has far less code to be implemented by the bean provider. The features that are common for all CMP entity beans are that they

  • Are implemented as an abstract class

  • Implement the EntityBean interface

  • Maintain an EntityContext attribute

  • Provide abstract getters and setters for attributes

  • Initialize the attributes by calling each of the setters from ejbCreate()

  • Implement stubs for ejbPostCreate() , ejbActivate() , ejbPassivate() , ejbLoad() , ejbStore() , and ejbRemove()

A CMP entity bean provides the abstract setters and getters for the container-managed fields of the entity bean. The EJB container performs the persistence of the fields identified in the deployment descriptor as a <cmp-field> . The container derives the type of the field from the type used with the getter and setter. For example, a name field of type String has the <cmp-field> in the deployment descriptor:

 
 <cmp-field> <field-name>name</field-name> <cmp-field> 

This corresponds to the abstract methods :

 
 abstract public String getName(); abstract public void setName( String name ) ; 

Creating the BookEntityCMPBean

Listing 21.17 shows the BookEntityCMPBean , which is an example of a CMP implementation of the BookEntity EJB. It is an abstract class with abstract setters and getters for the attributes of the entity EJB. The CMP fields for this entity bean are specified in the ejb-jar.xml deployment descriptor. The connection with the data source and the persistence of the container-managed fields are specified in the weblogic-ejb-jar.xml and weblogic-cmp-rdbms-jar.xml deployment descriptors. The deployment descriptors are described in the section, "Deployment on WebLogic Server," later in this chapter.

Listing 21.17 A CMP Entity Bean Is Implemented As an Abstract Class with Abstract Setters and Getters and Stubs for EJB Methods Provided by the Container
 /** * BookEntityCMPBean example of a CMP Entity EJB */ package com.objectmind.BookStore; import com.objectmind.ShoppingCart.*; import javax.ejb.*; import java.sql.SQLException; import java.sql.DriverManager; import java.sql.Connection; import java.sql.PreparedStatement; /** * @ejbHome <{BookEntityHome}> * @ejbRemote <{BookEntity}> * @ejbPrimaryKey <{BookEntityPK}> * @ejbTransactionAttribute Required * @persistent*/ // must implement the EntityBean interface public abstract class BookEntityCMPBean implements EntityBean { // the WebLogic Server will provide a context private EntityContext ctx; public void setEntityContext(EntityContext context) throws EJBException { // save the EntityContext ctx = context; } public void unsetEntityContext() throws EJBException { // clear the EntityContext ctx = null; } /** * abstract setters and getters for container-managed fields */ abstract public BookEntityPK getBookID(); abstract public void setBookID(BookEntityPK key); abstract public String getTitle(); abstract public void setTitle(String title); abstract public String getAuthor(); abstract public void setAuthor(String author); abstract public float getPrice(); abstract public void setPrice(float title); abstract public ShoppingCart getCart(); abstract public void setCart( ShoppingCart cart ) ; /** * stubs for container-managed behavior */ public void ejbPostCreate( BookEntityVO book ) { } public void ejbActivate() throws javax.ejb.EJBException { } public void ejbPassivate() throws javax.ejb.EJBException { } public void ejbRemove() throws RemoveException { } public void ejbStore() throws javax.ejb.EJBException { } public void ejbLoad() throws javax.ejb.EJBException { } /** * initialize the bean from the value object */ public BookEntityPK ejbCreate(BookEntityVO book) { setBookData( book ); return null; } /** * get the attributes of this entity bean as a value object */ public BookEntityVO getBookData(){ BookEntityVO value = new BookEntityVO(); BookEntityPK key = getBookID(); value.setBookID( key.bookID ); value.setAuthor( getAuthor() ); value.setTitle( getTitle() ); value.setPrice( getPrice() ); return value; } /* * set the attributes of this entity bean using the value object */ public void setBookData(BookEntityVO value) { BookEntityPK key = new BookEntityPK(); key.bookID = value.getBookID(); setBookID( key ); setAuthor( value.getAuthor() ); setTitle( value.getTitle() ); setPrice( value.getPrice() ); } } 


BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net