6.21. Developing the HibernateAccountingDAOThe 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.javapackage 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. |