Section 6.6. Iteration 1Introduce a Session Bean


6.6. Iteration 1Introduce a Session Bean

In 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.

  • Modify the Persistence Tier:

    • Add a STATUS column to the CAR table.

    • Make the CarDTO Serializable, and add a status field along with getter and setter methods.

    • Add a filterByStatus( ) method to the HibernateCarDAO.

  • Upgrade the web site:

    • Refactor the Controller Servlet's viewCarList action to use the InventoryFacadeBean for finding all available (unsold) cars.

    • Add a getEjbLocalHome( ) method to the ServiceLocator to look up an EJB's Local Home Interface using JNDI.

    • Add EJB-based JNDI reference settings to the Web deployment descriptors (web.xml and jboss-web.xml).

    • Automate EJB-based JNDI reference settings in the Web deployment descriptors with XDoclet.

  • Add a Session Bean:

    • Develop and deploy the InventoryFacadeBean.

6.6.1. Modifying the CAR Table

The 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 Field

Here 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.java
 package 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:

  • The CarDTO now implements the java.io.Serializable interface. We do this so that the CarDTO will serialize properly when remote clients call the InventoryFacadeBean. Remote access uses RMI, which requires objects (used as parameters or return values) to be serializable. Since all data members of the CarDTO are serializable, the only thing we need to do to make the DTO serializable is mark it as such. Java's default serialization mechanism takes care of the rest.

  • The code that calls the CarDTO will use the STATUS_SOLD and STATUS_AVAILABLE constants to get, set, and check a car's status.

  • Each constructor sets the status as "Available" to mark the car as unsold.

  • The getStatus( ) and setStatus( ) methods get and set the status. The @hibernate.property XDoclet tag for the getStatus( ) method associates the CarDTO's status data member with the STATUS column in the CAR table.

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 HibernateDAO

We'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.java
 public 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.



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