Components of an Entity Bean Using BMP

The components of an entity bean that uses BMP are the same as those of an entity bean that uses CMP:

  • Remote interface There is no change in how the remote interface is written for entity beans using either BMP or CMP.

  • Home interface There is no change in how the home interface is written for entity beans using either BMP or CMP.

  • Entity bean implementation class The major changes that need to be carried out to implement an entity bean using BMP occur in the entity bean's implementation class. To write an entity bean using BMP, the life-cycle methods that deal with persisting and maintaining the state of the entity bean need to be implemented. The following are the methods that need to be changed:

    • ejbCreate() In BMP you need to write the actual JDBC code that performs the insertion of the row in the database table. The values will be the parameters that have been passed to the ejbCreate() method.

    • ejbRemove() Because the ejbRemove() method call results in the deletion of the database row represented by the entity bean, you need to call the SQL statement that performs the delete operation in this method of the entity bean.

    • ejbLoad() The ejbLoad() method will contain the select SQL statement required to query the database and obtain the result set. This result set is then traversed and the values are extracted to refresh the contents of the entity bean.

    • ejbStore() You can write the JDBC code to update the database with the contents of the entity bean using the ejbStore() method. The update SQL statement with the required parameters should be executed to perform the update operation.

A code snippet of an example entity bean that uses these life cycle methods is given here:

 public interface MyBean implements javax.ejb.EntityBean  {     // create methods     public MyBeanPK ejbCreate(             String param1, String param2, int param3)             throws javax.rmi.RemoteException, javax.ejb.CreateException     {         conn = getConnection();         prepStmt = conn.prepareStatement(                 "<INSERT SQL STATEMENT> VALUES (?,?,?...)");         prepStmt.setString(1,param1);         prepStmt.setString(2,param2);         prepStmt.setInt(3,param3);         prepStmt.setXXX(n,paramn);         key = new MyBeanPK (param1);         return key;     } public MyBeanPK ejbCreateMyBusinessObject(         String param1, String param2, int param3)         throws javax.rmi.RemoteException, javax.ejb.CreateException     {         conn = getConnection();         prepStmt = conn.prepareStatement(                 "<INSERT SQL STATEMENT> VALUES (?,?,?...)");         prepStmt.setString(1,param1);         prepStmt.setString(2,param2);         prepStmt.setInt(3,param3);         prepStmt.setXXX(n,paramn);         key = new MyBeanPK (param1);         return key;     }     public void ejbLoad()     {         conn = getConnection();         prepStmt = conn.prepareStatement(                  "<SELECT SQL STATEMENT> col_name_1=?");         MyBeanPK key = (MyBeanPK)ctx.getPrimaryKey();         keyId = key.keyId;         prepStmt.setXXX(1, keyId);         ResultSet rs = prepStmt.executeQuery();         while(rs.next()){               val1 = rs.getXXX("col_name_1");               val2 = rs.getXXX("col_name_2");         ...         }     }     public void ejbStore()     {         conn = getConnection();         prepStmt = conn.prepareStatement(                  "<UPDATE SQL STATEMENT> col_name_1= ?, col_name_2 = ? ...                  WHERE key_id=?");         prepStmt.setXXX(1, val1);         prepStmt.setXXX(2, val2);         prepStmt.setXXX(3, this.keyID);         prepStmt.executeUpdate();     }     public void ejbRemove() throws RemoveException     {         conn = getConnection();         prepStmt = conn.prepareStatement(                 "<DELETE SQL STATEMENT> WHERE col_name_1 = ?");         prepStmt.setXXX(1, this.keyID);         prepStmt.executeUpdate();     }     // business methods     public void businessMethod1() throws javax.rmi.RemoteException     {         ... // business method functionality as required     }     public String businessMethod2() throws javax.rmi.RemoteException     {         ... // business method functionality as required     }     // finder methods     public ItemPK ejbFindByPrimaryKey(ItemPK primaryKey)             throws javax.rmi.RemoteException, javax.ejb.FinderException     {         conn = getConnection();         prepStmt = conn.prepareStatement("<SELECT SQL STATEMENT> col_name_1=?");         keyID = primaryKey.itemId;         prepStmt.setXXX(1, keyId);         ResultSet rs = prepStmt.executeQuery();         while(rs.next()){               keyId = rs.getInt("db_key_id");         }         return primaryKey;     }     public MyRemoteIF findXXX(String param1, int param2...)             throws javax.rmi.RemoteException, javax.ejb.FinderException     {         ...// finder functionality as required     }     // remove method     public void remove(String paramKey) throws             javax.rmi.RemoteException, javax.ejb.RemoveException     {     } } 

Next you will take a quick look at how BMP and CMP compare with each other.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

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