Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
The components of an entity bean that uses BMP are the same as those of an entity bean that uses CMP:
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. |