Developing Coarse-Grained Business Components
Vol 5 Issue 5 p.60
Listing 1: PersonalBean.ejbLoad
 
/**
* Loads the entity bean from the persistent storage.
* Checks whether or not to load dependent address objects
*
* @exception java.rmi.RemoteException
* if there is a communications
or systems failure
*/
public void ejbLoad() throws RemoteException {
System.out.println("ejbLoad ("+personID+")");
try {
// SQL actually performed in the read() method
read((PersonPK) ctx.getPrimaryKey());
// discard or reload any cached data, as per EJB 1.1
// spec, sec 9.1.7
if (lazyLoad.equals("true")) {
System.out.println("ejbLoad ("+personID+") - lazy load
addresses");
// ensures reset of address for this PersonBean!
addresses.clear();
}
else{
System.out.println("ejbLoad ("+personID+") -
loading addresses...");
getAddresses(); // loads the address list now
}
}
catch (FinderException fe) {
throw new RemoteException (fe.getMessage());
}
}

 

Listing 2: PersonBean.ejbStore
 
/**
* Stores the entity bean's PersonBean fields in the
* persistent storage.
* Also saves any modified Address objects.
*
* @exception java.rmi.RemoteException
* if there is a communications
or systems failure
*/
public void ejbStore() throws RemoteException {
System.out.println("ejbStore (" +personID+ ")");
Connection con = null;
PreparedStatement ps = null;
try {
con = getConnection();
ps = con.prepareStatement("update Person set title = ?, "+
"firstname = ?, "+
"lastname = ? "+
"where personid = ?");
ps.setString(1, title);
ps.setString(2, firstName);
ps.setString(3, lastName);
ps.setInt(4, personID);
int i = ps.executeUpdate();
if (i == 0) {
throw new RemoteException
("ejbStore: PersonBean ("+personID+") failed to update");
}
// update cached entity state (i.e. our addresses)
// as per EJB spec 1.1, sec: 9.1.7
Iterator list = addresses.iterator();
while (list.hasNext()) {
Address addr = (Address)list.next();
// only update if it was modified
if (addr.isModified()) {
sqlUpdateAddress(addr);
}
}
}
catch (RemoteException re) {
throw re;
}
catch (SQLException sqe) {
throw new RemoteException (sqe.getMessage());
}
finally {
try {
if (ps != null) ps.close();
if (con != null) con.close();
}
catch (Exception e) {
throw new RemoteException (e.getMessage());
}
}
}

 

Listing 3: PersonBean.ejbRemove
 
/**
* Deletes the entity bean and all addresses (dependent
* objects)from the persistent storage.
*
* @exception javax.ejb.RemoveException
* if the entity does not
allow removing the bean
* @exception java.rmi.RemoteException
* if there is a communications
or systems failure
*/
public void ejbRemove() throws RemoveException, RemoteEx-
ception {
System.out.println("ejbRemove ("+personID+")");
addresses.clear(); // reset
Connection con = null;
PreparedStatement ps = null;
try {
con = getConnection();
// get the PK from the context. This is because ejbLoad
// does not have to be called (see sequence diagrams in
// EJB spec1.1) on an ejbRemove() and using the personID
// at this point may give the personID of a different bean!
PersonPK pk = (PersonPK) ctx.getPrimaryKey();
ps= con.prepareStatement("delete from person where person
ID = ?");
ps.setInt(1, pk.personID);
int i = ps.executeUpdate();
if (i == 0) {
throw new RemoteException ("ejbRemove : PersonBean ("+pk.personID+ ") not found");
}
ps.close();
// delete all addresses for the personBean
ps = con.prepareStatement("delete from address where per-
sonID = ?");
ps.setInt(1, pk.personID);
ps.executeUpdate();
}
catch (SQLException sqe) {
throw new RemoteException (sqe.getMessage());
}
finally {
try {
if (ps != null) ps.close();
if (con != null) con.close();
}
catch (Exception e) {
throw new RemoteException (e.getMessage());
}
}
}onBean.ejbRemove