5.7. Hibernate ChecklistBefore we get to the actual HibernateCarDAO , let's recap what we've done up to this point. We:
|
5.8. HibernateCarDAOIt's taken a while to get here, but now that the infrastructure is in place, we can get to the whole point of this chapterseeing Hibernate in action. In JDBCCarDAO , we performed the SQL query and manually marshaled the ResultSet rows into CarDTO objects. Example 5-13 shows what the JDBC code looks like. Example 5-13. JDBCCarDAO.java
private static final String DATA_SOURCE="java:comp/env/jdbc/JBossAtWorkDS";
public List findAll( )
{
List carList = new ArrayList( );
DataSource dataSource = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
dataSource = ServiceLocator.getDataSource(DATA_SOURCE);
conn = dataSource.getConnection( );
stmt = conn.createStatement( );
rs = stmt.executeQuery("select * from CAR");
while(rs.next( ))
{
CarDTO car = new CarDTO( );
car.setId(rs.getInt("ID"));
car.setMake(rs.getString("MAKE"));
car.setModel(rs.getString("MODEL"));
car.setModelYear(rs.getString("MODEL_YEAR"));
carList.add(car);
}
}
catch (Exception e)
{
System.out.println(e);
}
finally
{
try
{
if(rs != null){rs.close( );}
if(stmt != null){stmt.close( );}
if(conn != null){conn.close( );}
}
catch(Exception e)
{
System.out.println(e);
}
}
return carList;
}
It's nearly 50 lines of code, if you include all the exception handling. The point of showing this to you is to
Now let's see the same method implemented with Hibernate in Example 5-14. Example 5-14. HibernateCarDAO.java
private static final String
HIBERNATE_SESSION_FACTORY="java:comp/env/hibernate/SessionFactory";
public List findAll( )
{
List carList = new ArrayList( );
Session session = null;
try
{
session = ServiceLocator.getHibernateSession(HIBERNATE_SESSION_FACTORY);
Criteria criteria = session.createCriteria(CarDTO.class);
carList = criteria.list( );
}
catch (Exception e)
{
System.out.println(e);
}
finally
{
try
{
if (session != null) {session.close( );}
}
catch (Exception e)
{
System.out.println(e);
}
}
return carList;
}
It is far more compactwith a nearly 50% reduction in total lines of code. More importantly, the code doing the real work was reduced to three steps:
The ResultSet row marshalling still happens, but the Hibernate API takes care of it behind the scenes. Our code is lean and clean. We'll explore some more in depth Hibernate examples in just a bit, and will cover the full CRUD spectrum (Create, Read, Update, and Delete). But for now, let's change our ControllerServlet and deploy the application.
To begin using
HibernateCarDAO
, all we need to do is change how the
CarDAO
interface is
Example 5-15. ControllerServlet.java
if(VIEW_CAR_LIST_ACTION.equals(actionName))
{
CarDAO carDAO = new HibernateCarDAO( );
request.setAttribute("carList", carDAO.findAll( ));
destinationPage = "/carList.jsp";
}
Now that everything is in place, let's build and deploy the application:
Click on the View Inventory link to see Figure 5-1. Figure 5-1. viewCarList using Hibernate
OK, so we've been through at least five distinct iterations that have all given us the same result:
In each case, the view remained constantonly the back-end services have gotten progressively more sophisticated. For
|