A Struts 2, Spring, and Hibernate Example Application

Introduction

I was learning Hibernate as another methodology for storing my Java objects' state in a data repository. I wanted to experiment with integrating Hibernate into a Struts 2 and Spring application so I created an example of using the three technologies together. The combination of these three frameworks enables Java developers to create powerful web applications with a minimum of code.

Example Application

Download the example application.

The example application was created using Eclipse 3.5 with the Maven 2 plugin. The JDK used was JVM 1.6 on the Mac. The application uses Maven to manage its Jar dependencies. Please note the example application is partially based on one of the Hibernate examples provided in Cameron McKenzie's excellent Hibernate Made Easy book.

The application uses an in-memory database (HSQL) that is setup by the Spring applicationContext.xml and the TestDataSourceFactory class. See files schema.sql and test-data.sql in src/main/resources for the schema created during the application's startup and the initial records inserted into the table's database. Using the in-memory database and the TestDataSourceFactory class means there is no need to setup and run a separate database server for the example application.

You can import this project into Eclipse by using Eclipse's File - Import feature. In the project's root folder is a README.txt file that explains how to run the application.

If you're not using Eclipse 3.5 with the Maven 2 plugin, you can unzip the archived download and view the source code in any text editor. If you are using a Java IDE that enables importing of Maven projects you should be able to import the unzipped project.

You can also run the application using Maven directly if you have downloaded and installed Maven on your computer. Open a terminal (command) window and navigate to the project's root folder (the folder where pom.xml resides). Then in the terminal window issue these commands:

mvn clean
mvn test (All 7 tests should pass successfully).
mvn jetty:run

When you see [INFO] Started Jetty Server open a web browser and navigate to http://localhost:8080/index.html.

To stop the Jetty web server type CTRL-C in the terminal window.

Implementing Hibernate

To implement Hibernate, I use the Java Persistence API (JPA) annotations (@Entity, @Column, @Table, etc) to provide Hibernate the information it needs to map my model object to a table and columns in the database schema. I also have my Data Access Object (DAO) extend Spring's HibernateDaoSupport class so that I can easily inject a Hibernate SessionFactory into the DAO. I also use Spring's @Transactional annotation to mark the methods of my DAO as occurring in a single transaction.

Note there are several different ways to integrate Spring and Hibernate. My example's methodology may not be the best one. Consult the Spring references below for other ways to use Spring and Hibernate together.

Integrating Struts 2, Spring, and Hibernate

The key to effectively using Struts 2 with Hibernate is to properly layer (Presentation, Action, Service, DAO) your application. In my example, the Struts 2 ActionSupport classes are ignorant of how Java objects are provided. The Struts 2 ActionSupport class makes calls to a Service class (injected by Spring). The Service class makes calls to the DAO class (injected by Spring) which uses Hibernate to get data out of and into the data repository and to translate the data into and out of Java objects.

All concrete classes implement an Interface and any class referenced as part of another class is referenced by its Interface type. This makes the overall application easier to maintain, test, and enhance in the future.

The concrete HibernateDaoSupport classes receive their Hibernate SessionFactory objects injected by Spring. The SessionFactory object receives its DataSource object injected by Spring. See the Spring applicationContext.xml for the creation of these Spring-managed beans.

Summary

There is a steep learning curve in becoming comfortable using these complex frameworks. You're not going to learn Struts 2, Spring, and Hibernate overnight. Leveraging these technologies requires studying good books, online references, and example applications. The references I've listed below, are ones I've found useful in learning Struts 2, Spring, and Hibernate.

Of course this blog article is not going to be able to teach anyone everything (anything?) involved in integrating these three technologies. Consult the references below for where to go to learn more. I do hope my example application will be useful to others learning Hibernate and who want to integrate Hibernate into a Struts 2 and Spring application.

References:

  1. Hibernate Made Easy, Cameron McKenzie, 2008, http://www.hiberbook.com/
  2. Hibernate Documentation, https://www.hibernate.org/5.html
  3. Spring Recipes (chapter 9), Gary Mak, 2008 Apress, http://www.apress.com/book/view/9781590599792
  4. Pro Spring 2.5 (chapter 11), Jan Machacek, Aleksa Vukotic, and others, 2008 Apress, http://www.apress.com/book/view/9781590599211
  5. Spring Framework - http://www.springsource.org/documentation
  6. Spring Community Forums - http://forum.springsource.org/
  7. Introduction to the Spring Framework 2.5 - http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25
  8. Spring in Action, 2nd Edition, Manning Publishing, August 2007
  9. Struts 2 Framework - http://struts.apache.org
  10. Using Struts 2 and Spring Frameworks Together, Bruce Phillips Blog - /blog/index.cfm/2008/10/17/Using-Struts-2-and-Spring-Frameworks-Together
  11. Struts 2 In Practice, Manning Publishing, http://www.manning.com/wannemacher/
  12. Apache Struts 2 Documentation, Spring Plugin - http://struts.apache.org/2.x/docs/spring-plugin.html
  13. Struts 2 In Action, Donald Brown, 2008 Manning, http://www.manning.com/dbrown/
  14. Apache Struts 2 Web Application Development, Dave Newton, 2009 Packt, http://www.packtpub.com/apache-struts-2-web-application-development-beginners-guide/book
  15. Struts User Forum - http://www.nabble.com/Struts---User-f206.html
  16. Maven - http://maven.apache.org/
  17. Maven - http://books.sonatype.com/maven-book/index.html

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner