#眉標=J2EE #副標=J2EE的商業層(2) #大標=EJB的生命週期與關聯 #作者=文/陳俊吉   ==================================    public class HelloBMPBean implements EntityBean { private EntityContext ctx = null; private DataSource ds = null; private String id = null; private String name = null; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public HelloBMPBean() { } public String ejbCreate(String id, String name) { this.id = id; this.name = name; Connection conn = null; PreparedStatement ps = null; try { conn = ds.getConnection(); ps = conn.prepareStatement("INSERT INTO helloBMP (id, name) VALUES (?, ?)"); ps.setString(1, id); ps.setString(2, name); ps.executeUpdate(); return id; } catch (Exception e) { throw new EJBException(e); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { throw new EJBException(e); } } } } public void ejbPostCreate(String id, String name) throws EJBException { }   =====================================    ======================================== public String ejbFindByPrimaryKey(String pk) throws FinderException { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = ds.getConnection(); ps = conn.prepareStatement("SELECT id FROM helloBMP WHERE id = ?"); ps.setString(1, pk); rs = ps.executeQuery(); if (rs.next()) { return pk; } else { throw new EJBException("Row not found: " + pk); } } catch (Exception e) { throw new EJBException(e); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { throw new EJBException(e); } } } } public void ejbLoad() throws EJBException { String pk = (String) ctx.getPrimaryKey(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = ds.getConnection(); ps = conn.prepareStatement("SELECT id, name FROM helloBMP WHERE id = ?"); ps.setString(1, id); rs = ps.executeQuery(); if (rs.next()) { id = rs.getString("orderId"); name = rs.getString("orderName"); } else { throw new EJBException("Row not found: " + pk); } } catch (Exception e) { throw new EJBException(e); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { throw new EJBException(e); } } } } =======================================   ======================================= public void ejbStore() throws EJBException { Connection conn = null; PreparedStatement ps = null; try { conn = ds.getConnection(); ps = conn.prepareStatement("UPDATE helloBMP SET id = ?, name = ? WHERE id = ?"); ps.setString(1, id); ps.setString(2, name); ps.setString(3, id); ps.executeUpdate(); } catch (Exception e) { throw new EJBException(e); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { throw new EJBException(e); } } } } public void ejbRemove() throws RemoveException, EJBException { Connection conn = null; PreparedStatement ps = null; try { conn = ds.getConnection(); ps = conn.prepareStatement("DELETE FROM helloBMP WHERE id = ?"); ps.setString(1, id); ps.executeUpdate(); id = null; name = null; } catch (Exception e) { throw new EJBException(e); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { throw new EJBException(e); } } } } ============================================    ======================================= public void ejbActivate() throws EJBException { } public void ejbPassivate() throws EJBException { } public void setEntityContext(EntityContext entityContext) throws EJBException { this.ctx = entityContext; try { Properties props = new Properties(); Context ctx = new InitialContext(props); ds = (DataSource) ctx.lookup("java:DefaultDS"); } catch (Exception e) { throw new EJBException(e); } } public void unsetEntityContext() throws EJBException { } =======================================    ===================================== public interface HelloBMPHome extends EJBHome { public HelloBMP findByPrimaryKey(String pk) throws RemoteException, FinderException; public HelloBMP create(String id, String name) throws CreateException, RemoteException; } =========================== =============================== public interface HelloBMP extends EJBObject { public String getId() throws RemoteException; public void setId(String id) throws RemoteException; public String getName() throws RemoteException; public void setName(String name) throws RemoteException; } =============================== =========================== public class CustomerBean implements EntityBean { private String id; private String name; private Collection orders; ……前略 public void ejbLoad() { //JDBC SELECT Customer //JNDI lookup Order //Call OrderBean's findByCustomer } public void ejbStore() { //JDBC UPDATE Customer //JDBC UPDATE Order } public Collection getOrders() { return orders; } public void setOrders(Collection c) { this.orders = c; } } =============================     =========================== public abstract class CustomerBean implements EntityBean { ……前略 public void ejbLoad() throws EJBException { } public void ejbStore() throws EJBException { } public abstract Collection getOrders(); public abstract void setOrders(Collection orders); }   ===========================   實作CMR的關鍵在於部署檔的內容,來看看詳細的內容: ……前略 CustomerEJB LocalCustomerHome LocalCustomer CustomerBean Container java.lang.String False 2.x id name id OrderEJB LocalOrderHome LocalOrder OrderBean Container java.lang.String False 2.x id name id One CustomerEJB orders java.util.Collection Many OrderEJB 後略…… ===========================================    ========================================= ……前略 Many OrderEJB customer 後略…… ===============================      ============================== public abstract class OrderBean implements EntityBean { ……前略 public abstract String getId(); public abstract void setId(String id); public abstract String getName(); public abstract void setName(String name); public String ejbCreate(String id, String name) throws CreateException { this.setId(id); this.setName(name); return null; } public void ejbPostCreate(String id, String name) throws CreateException { } public abstract LocalCustomer getCustomer(); public abstract void setCustomer(LocalCustomer customer); } =============================================    ==================================== public interface LocalOrder extends EJBLocalObject { public LocalCustomer getCustomer(); public void setCustomer(LocalCustomer customer); public String getId(); public void setId(String id); public String getName(); public void setName(String name); } =================================    ======================================= HelloMDBEJB queue/testQueue CustomerEJB CustomerEJB OrderEJB OrderEJB ==========================================