6.6. Iteration 1Introduce a Session BeanIn this Iteration, we'll take the following steps to introduce a Session Bean to the JAW Motors application and lay the groundwork for the rest of the chapter.
6.6.1. Modifying the CAR TableThe CAR table's new STATUS column indicates whether a car is "Sold" or "Available." Example 6-1 shows the new SQL in ch06-a/sql/build.xml used to create the table. Example 6-1. sql/build.xml CREATE TABLE CAR ( ID BIGINT identity, MAKE VARCHAR(50), MODEL VARCHAR(50), MODEL_YEAR VARCHAR(50), STATUS VARCHAR(10) ); INSERT INTO CAR (ID, MAKE, MODEL, MODEL_YEAR, STATUS) VALUES (99, 'Toyota', 'Camry', '2005', 'Available'); INSERT INTO CAR (ID, MAKE, MODEL, MODEL_YEAR, STATUS) VALUES (100, 'Toyota', 'Corolla', '1999', 'Available'); INSERT INTO CAR (ID, MAKE, MODEL, MODEL_YEAR, STATUS) VALUES (101, 'Ford', 'Explorer', '2005', 'Available'); A car is considered "Available" when you create it. Now let's upgrade the CarDTO. 6.6.2. Upgrading the CarDTO with a Status FieldHere we'll make the CarDTO Serializable and add a new status data member along with corresponding setter and getter methods. Example 6-2 shows the changes. Example 6-2. CarDTO.javapackage com.jbossatwork.dto; import java.io.*; /** * @hibernate.class * table="CAR" */ public class CarDTO implements Serializable { public static final String STATUS_AVAILABLE = "Available"; public static final String STATUS_SOLD = "Sold"; ... private String status; public CarDTO( ) { ... this.status = CarDTO.STATUS_AVAILABLE; } public CarDTO(String make, String model, String modelYear) { ... this.status = CarDTO.STATUS_AVAILABLE; } public CarDTO(int id, String make, String model, String modelYear) { ... this.status = CarDTO.STATUS_AVAILABLE; } ... /** * @hibernate.property * column="STATUS" */ public String getStatus( ) { return status; } public void setStatus(String status) { this.status = status; } } Here's a breakdown of the modifications to the CarDTO:
Now that we've upgraded the database and the CarDTO to hold status information, we'll add a new method to the HibernateCarDAO that looks at the status. 6.6.3. Adding filterByStatus( ) to the HibernateDAOWe've added the filterByStatus( ) method that returns a List of all cars in the inventory whose status matches the value of the caller-supplied status parameter. Example 6-3 shows the changes to the HibernateCarDAO. Example 6-3. HibernateCarDAO.javapublic class HibernateCarDAO implements CarDAO { ... public List filterByStatus(String status) { List availableCarList = new ArrayList( ); Session session = null; try { session = ServiceLocator.getHibernateSession( HIBERNATE_SESSION_FACTORY); Criteria criteria = session.createCriteria(CarDTO.class) .add( Restrictions.eq("status", status) ); availableCarList = criteria.list( ); } catch (Exception e) { System.out.println(e); } finally { try { if (session != null) {session.close( );} } catch (Exception e) { System.out.println(e); } } return availableCarList; } ... } The new filterByStatus( ) method looks similar to the findAll( ) method from the Hibernate chapter. The only difference is that we use a Restriction to limit the result set to those cars whose status matches the value of the fiterByStatus( ) method's status parameter. Think of a Hibernate Restriction like a SQL WHERE clause. We've upgraded everything related to the database, and now we're going to modify the Controller Servlet to call the InventoryFacadeBean. |