Deleting an Entity

   

A client can call either of the two remove methods of the home interface or the component interface's remove to delete an entity object. The container responds to this request by calling your ejbRemove method for the instance that needs to be deleted. You're responsible for doing whatever is necessary to remove the entity from the database within this method. Listing 6.13 shows the ejbRemove method for EnglishAuctionBean .

Listing 6.13 ejbRemove “A BMP ejbRemove Method Deletes an Entity from the Database
 package com.que.ejb20.auction.model;  ...  public class EnglishAuctionBean extends AbstractEntity implements EntityBean {   ...    public void ejbRemove() throws RemoveException {     // an open auction has to be closed or cancelled      // before it's allowed to be deleted      if (IAuctionStatus.AUCTION_OPEN.equals(getStatus())) {       throw new RemoveException("Cannot delete an open auction");      }      Connection con = null;      PreparedStatement stmt = null;      try {       con = BMPHelper.getConnection("auctionSource");        // do all deletes based on the primary key        Integer primaryKey = (Integer)ctx.getPrimaryKey();        // delete the auction's bids first        if (!getBids().isEmpty()) {         int numBids = getBids().size();          // build a prepared statement and delete the dependent bid objects          stmt = con.prepareStatement("DELETE FROM bid WHERE auction_id = ?");          stmt.setInt(1, primaryKey.intValue());          // perform the delete and throw an exception if it fails          int rowsDeleted = stmt.executeUpdate();          if (rowsDeleted != numBids) {           throw new EJBException("Error deleting bids for auction " + id);          }        }        // now build a prepared statement to delete the auction        stmt = con.prepareStatement("DELETE FROM auction WHERE id = ?");        stmt.setInt(1, primaryKey.intValue());        // perform the delete and throw an exception if it fails        int rowsDeleted = stmt.executeUpdate();        if (rowsDeleted != 1) {         throw new EJBException("Error deleting auction " + id);        }      }      catch (SQLException e) {       // throw a system exception if a database access error occurs        throw new EJBException;      }      finally {       // close the connection        BMPHelper.cleanup(stmt, con);      }    }    ...  } 

You'll learn more about exception handling in Chapter 13, but Listing 6.13 demonstrates that you're allowed to throw an application exception from an ejbRemove method to, in effect, veto a client request to delete an entity. Here, ejbRemove is implemented to enforce a business rule that requires an active auction to be closed or cancelled instead of being deleted while it's still in progress. If the auction is in a valid state for removal, its bids are deleted and then the auction itself is deleted.



Special Edition Using Enterprise JavaBeans 2.0
Special Edition Using Enterprise JavaBeans 2.0
ISBN: 0789725673
EAN: 2147483647
Year: 2000
Pages: 223

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