Section 6.21. Developing the HibernateAccountingDAO


6.21. Developing the HibernateAccountingDAO

The HibernateAccountingDAO looks similar to the HibernateCarDAO, but instead of writing all the CRUD, we only need to insert a row into the ACCOUNTING table. Here, we have only a single create( ) method. Example 6-29 shows the code for the HibernateAccountingDAO.

Example 6-29. HibernateAccountingDAO.java
 package com.jbossatwork.dao; import java.util.*; import org.hibernate.*; import org.hibernate.criterion.*; import com.jbossatwork.dto.AccountingDTO; import com.jbossatwork.util.*; public class HibernateAccountingDAO implements AccountingDAO {     private static final String HIBERNATE_SESSION_FACTORY =                              "java:comp/env/hibernate/SessionFactory";     public HibernateAccountingDAO(  ) {  }     public void create(AccountingDTO accountingData)     {         Session session = null;         try         {             session = ServiceLocator.getHibernateSession(                                   HIBERNATE_SESSION_FACTORY);             session.save(accountingData);         }         catch (Exception e)         {             System.out.println(e);         }     }  } 

The create( ) method calls the ServiceLocator to get the Hibernate Session, and inserts a new row into the ACCOUNTING table by calling the Session's save( ) method for the AccountingDTO. Just like we did before, we let the container manage the transaction, and we're not closing the Session.

We've added all the infrastructure to the web site and the Persistence Tier so we can buy a car. We now wrap up by adding a buyCar( ) method to 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