reset password
Author Message
cysun
Posts: 2935
Posted 15:01 Dec 14, 2009 |

In Part I we have created a Spring-based web application, and here we add Hibernate to the mix:

1. Copy the necessary library jar files to WEB-INF/lib.

2. Create the "trinity" of Hibernate: model class, mapping file, and database. The mapping file should be somewhere under /src.

3. Create hibernate.cfg.xml and place it under /src.

4. Create the DAO interface and DAO implementation class. Although you do not have to follow the DAO design pattern, there is really no reason not to. Also to take advantage of Spring's automatic transaction management and other Hibernate support, the DaoImpl class should inherite from HibernateDaoSuport.

5. Modify the controller and the JSP so that the page will display something from the database.

6. Modfiy hello-servlet.xml. You need to add/modify the following beans:

  • dataSource, sessionFactory, transactionManager, transactionAttriuteSource. These are kind of the standard beans needed for all applications. The easiest way is to simply copy them from the file conf/spring-data.xml in the CSNS project. Modify the dataSource bean so that it matches your database setup.
  • add the DAO bean.
  • wire the DAO bean to the Hello bean.

7. Run the project.

cysun
Posts: 2935
Posted 15:25 Dec 14, 2009 |

There's something called OpenSessionInViewInterceptor. It keeps a Hibernate session open until the view is rendered; without it you may run into some problems due to Hibernate's lazy loading behavior. Although we didn't use it in our HelloWorld example, mostly likely you'll need it for your project. Check out conf/spring-servlet.xml in the CSNS project for how to configure this interceptor.