Section 6.22. Adding buyCar( ) to the InventoryFacadeBean


6.22. Adding buyCar( ) to the InventoryFacadeBean

Example 6-30 is the InventoryFacadeBean's buyCar( ) method.

Example 6-30. InventoryFacadeBean.java
 ... public class InventoryFacadeBean implements SessionBean {     ...     /**      * @ejb.interface-method      * @ejb.transaction      *  type="Required"      *      */     public void buyCar(int carId, double price) throws EJBException {         CarDAO carDAO = new HibernateCarDAO(  );         CarDTO car;         AccountingDAO accountingDAO = new HibernateAccountingDAO(  );         AccountingDTO accountingData;         car = carDAO.findById(carId);         car.setStatus(CarDTO.STATUS_SOLD);         carDAO.update(car);         accountingData = new AccountingDTO(carId, price);         accountingDAO.create(accountingData);     }     ... } 

The buyCar( ) method encapsulates a transaction for buying a car. This method takes the carId and price supplied by the caller to mark a car as "Sold" in the CAR table and record the sale in the ACCOUNTING table. If either the update to the CAR table or the ACCOUNTING table fails, the container rolls everything back. If all activities complete successfully, then all changes are committed to the database. After instantiating the DAOs, we use the CarDAO to find the car using the carId. To mark the car as "Sold", we set the CarDTO's status to "Sold" and call the HibernateCarDAO's update( ) method to update the car's status in the CAR table. To record the sale, we instantiate a new AccountingDTO with the carId and price, and then call the HibernateAccountingDAO's create( ) method to insert a new row in the ACCOUNTING table.



JBoss at Work. A Practical Guide
JBoss at Work: A Practical Guide
ISBN: 0596007345
EAN: 2147483647
Year: 2004
Pages: 197

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