Put together Struts2, JPA, Hibernate and Spring

This weekend I played a bit with Struts2. Obviously my first thought was to put together “fellowship of the ring” ie Struts, Spring and Hibernate. Now, we have two ways for integrating Hibernate and I’ve chosen the Java Persistence Architecture approach, because is the latest trend in terms of persistence in the J2EE landscape.

Create your domain model

For the sake of simplicity I’ll take  a simple example that contains Books and Authors. A book has one or more authors while one author wrote one or more books. This looks like a many to many relationship and we want to be able to get, from one shoot, the book and its authors and for an author his entire list of books.


package org.bserban.shs.model;
import javax.persistence.*;
import java.util.List;
@Table(name = "book")
public class Book {
    private Integer id;
    private String title;
    private String publisher;
    private String keywords;
    private String ISBN13;
    private String ISBN10;
    @ManyToMany(fetch = FetchType.EAGER)
    private List<Author> authors;

Author. java

package org.bserban.shs.model;
import javax.persistence.*;
import java.util.List;
@Table(name = "author")
public class Author {
    private Integer id;
    private String firstName;
    private String lastName;
    private String blog;
    @ManyToMany(mappedBy="authors",fetch = FetchType.EAGER)
    private List<Book> books;

As you see in the code snippets above, the many to many relationship was marked with @ManyToMany annotation. We specified that the many to many relationship is mapped by Author entity too. No need to create a persistence.xml or hibernate.cfg.xml files at this moment because the whole configuration will take place in the Spring files.

Create a DAOs

First of all create a generic DAO object to keep the reference to the EntityManager and avoid duplicate code in other DAOs.

package org.bserban.shs.dao;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public abstract class GenericJPADAO<T, ID extends Serializable> {

private Class<T> persistentClass;

protected EntityManager em;

public void setEntityManager(EntityManager em) {
this.em = em;

public EntityManager getEntityManager() {
return em;

public void save(T entity) {

public void delete(T entity) {
public T getById(Object id) {
return getEntityManager().find(persistentClass, id);

By making a generic DAO class using java generics we are avoiding casting and using the Object class as parameter, in this way our methods will always have the proper type for their operation. While this is very easy with simple application it can become more complicated, but don’t forget that now we are building bricks that will help us to build our house, for more complicated logic we just simply create a facade class that will use our bricks.
BookDAO.java and AuthorDAO.java

package org.bserban.shs.dao;
import org.bserban.shs.model.Author;
public class AuthorDAO extends GenericJPADAO<Author,Integer> {
package org.bserban.shs.dao;
import org.bserban.shs.model.Book;
public class BookDAO extends GenericJPADAO<Book,Integer>{

These classes are empty so far but specific methods will probably be added.

Create your service layer

I am going to build also a generic service similar with the generic DAO class. For this example I only need basic operations that can be easily made generic. All I need is to create a generic interface called GenericService and ask for a type. Note that while GenericService is an interface for the DAOs I’ve done an abstract class because I wanted to add code to methods.

package org.bserban.shs.service;
import java.util.List;
public interface GenericService<T> {
    T create(T entity);
    void delete(T entity);
    T update(T entity);
    List<T> list(int page, int size);

Now, in the Services classes we need to have also a reference to the specific DAO object for that type of service. I am going to create a dao field in each class and I am going to assume that somebody will set the dao to the correct value when the service will be used.

BookService.java and AuthorService.java, their structure is identically so i will list only one here:

package org.bserban.shs.service;
import org.bserban.shs.dao.AuthorDAO;
import org.bserban.shs.model.Author;
import java.util.List;
public class AuthorService implements GenericService<Author> {
    private AuthorDAO dao;
    public Author create(Author entity) {
        return entity;
    public void delete(Author entity) {
    public Author update(Author entity) {
        return entity;
    public List<Author> list(int page, int size) {
        return dao.list(page,size);
    public AuthorDAO getDao() {
        return dao;
    public void setDao(AuthorDAO dao) {
        this.dao = dao;

For the listing feature I simply added a new method in the DAOs for list, and it looks like this:

public List<Author> list(int page, int size) {
    Query query = this.em.createQuery("from Author order by lastName,firstName");
    query.setFirstResult((page - 1) * size);
    return query.getResultList();
public List<Book> list(int page, int size) {
    Query query = this.em.createQuery("from Book order by title");
    query.setFirstResult((page - 1) * size);
    return query.getResultList();

Create the Actions

The actions are part of the controller and they need to validate data and to provide error handling before calling the service layer. One characteristic of putting together the Spring and Struts2 is that the dependency injection is done via a constructor argument. You could do it also with setters and getters but this would expose your service class to the exterior. One could call your action via HTTP by providing a parameter with the same name of your service class which will result into an unpredictable behavior, most probably an error.

In the actions I am going to have three fields, the first is the Service reference, the second is the list of model object and the third is a solo entity used for create, update, delete.

I will only list one Action class because they are pretty similar:

public class AuthorAction extends FrontendAction {
    private GenericService<Author> service;
    private List<Author> authors;
    private Author author;
    public AuthorAction(GenericService<Author> service) {
        this.service = service;
    public String list() {
        authors=service.list(page, size);
        return SUCCESS;
    public String update() {
        return SUCCESS;
    public String create() {
        return SUCCESS;
    public String delete() {
        return SUCCESS;

I have created a FrontendAction to put the page and size parameters, since they are a common feature of both actions.  Similar, you can put here other attributes that are common for the all actions.

Now lets add our actions into struts.xml file and start to configure the Spring support. I have chosen the extension of our actions to be *.html. I also want to set the Struts2 into dev mode to debug the application if necessary. For IOC Struts2 has a property called objectFactory which let’s you to specify what is your IOC container, i set this to “spring” values. The views will be written as Freemarker templates which I consider to be a powerful template engine.

<constant name="struts.devMode" value="true"/>
<constant name="struts.action.extension" value="html"/>
<constant name="struts.objectFactory" value="spring"/>
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.ui.templateSuffix" value="ftl"/>
<constant name="struts.configuration.xml.reload" value="true"/>

Now we need to declare our actions, I’ve decided to create different name space for each action which means that the http calls will look like this: /books/…..   /authors/…..

<package name="books" namespace="/books" extends="struts-default">
    <action name="list" class="bookAction" method="list">
        <result name="success" type="freemarker">/WEB-INF/pages/books/list.ftl</result>
<package name="authors" namespace="/authors" extends="struts-default">
    <action name="list" class="authorAction" method="list">
        <result name="success" type="freemarker">/WEB-INF/pages/authors/list.ftl</result>

For the purpose of demonstration I am providing only the listing capability, I let you code the rest. So I need to create two templates to list the entities, they are done in the same way so i am pasting only the author’s list page:

<link media="screen" type="text/css" href="${base}/resources/css/style.css" rel="stylesheet">
<table class="listing">
    <tr><td class="hed" colspan="3">List of authors</td></tr>
        <th>First Name</th>
        <th>Last Name</th>
    [#list authors as author]
    <tr [#if author_index%2 = 0]class="bg"[/#if]>
        <td>[#if author.books??][#list author.books as book] ${book.title}[#if book_has_next],[/#if][/#list][/#if]</td>

Glue the code with Spring

This example uses Spring 2.5.5 version. I am going to separate the configuration files in two, one is for generic configuration for JPA and transactions and the second will contain our classes glued together.

My applicationContext.xml

Declare the JPA support

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

Now declare the EntityManager bean

<bean id="entityManagerFactory"
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="showSql" value="true" />

The data source used by EntityManager bean

<bean id="dataSource"
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/example" />
    <property name="username" value="example" />
    <property name="password" value="example" />

Now I need to configure the transaction manager for my classes and obviously I am going to use the one that supports annotations. The era for declarative transaction configuration has long gone.

<bean id="transactionManager"
        <property name="entityManagerFactory" ref="entityManagerFactory" />
<tx:annotation-driven transaction-manager="transactionManager" />

Let’s not forgot our objects, let’s tell Spring where is our application specific configuration file

<import resource="exampleContext.xml"/>
Now, all I need is to glue my classes together, basically our DAOs need the JPA beans, the Services need our DAOs and Struts Actions need our Services.
exampleContext.xml will contain the followings:
<!-- daos -->
 <bean id="authorDao" class="org.bserban.shs.dao.AuthorDAO"/>
 <bean id="bookDAO" class="org.bserban.shs.dao.BookDAO"/>
 <!-- services -->
 <bean id="authorService" class="org.bserban.shs.service.AuthorService">
     <property name="dao" ref="authorDao"/>
 <bean id="bookService" class="org.bserban.shs.service.BookService">
     <property name="dao" ref="bookDAO"/>
 <!-- action -->
 <bean id="authorAction" scope="prototype" class="org.bserban.shs.action.AuthorAction">
     <constructor-arg ref="authorService"/>
 <bean id="bookAction" scope="prototype" class="org.bserban.shs.action.BookAction">
     <constructor-arg ref="bookService"/>

Now add the transaction support to our service classes. I simply need to put the @Transactional annotation at class level which means that all method from that class support transactions.

import org.springframework.transaction.annotation.Transactional;
public class BookService implements GenericService<Book> {

I am going to configure also the web.xml file, where I’ll put the configuration for Struts and for Spring.


Now all we need is to hit run and see if it is working. Also we are going to need some testing data.

Database tables

Putting this line <property name=”generateDdl” value=”true” /> into EntintyManagerFactory bean configuration will cause the generation of our database tables.


I just need to insert manually some sample data to be able to list them in the “list” pages.

Sample data:

INSERT INTO `author` (`id`,`firstName`,`lastName`,`blog`) VALUES  (1,'John','Doe','no blog'), (2,'Sam','Dune','no blog');
INSERT INTO `book` (`id`,`title`,`publisher`,`keywords`,`ISBN13`,`ISBN10`) VALUES  (1,'Some Random Title','The plublisher','random','0596517726','978-0596517724');
INSERT INTO `book_author` (`books_id`,`authors_id`) VALUES  (1,1);

Html Views

Let’s add a simple css file to give a decent look to our pages, all we need is to customize the table look&feel.

table.listing {
    text-align: center;
    font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
    font-weight: normal;
    font-size: 11px;
    color: #fff;
    width: 280px;
    background-color: #666;
    border: 0px;
    border-collapse: collapse;
    border-spacing: 0px;
table.listing td {
    background-color: #CCC;
    color: #000;
    padding: 4px;
    text-align: left;
    border: 1px #fff solid;
table.listing td.hed {
    background-color: #666;
    color: #fff;
    padding: 4px;
    text-align: left;
    border-bottom: 2px #fff solid;
    font-size: 12px;
    font-weight: bold;

End the result:





I hope I’ll have the time to continue this apps and introduce jQuery for simple actions like add/edit/delete/filter books and authors.



I’ll list here all the needed jars/frameworks for this sample application:

Source code sample, you can downloaded from here Struts2, Spring, JPA, Hibernate

Simple MVC for Flex and AIR

This weekend I tried to make an “Air” client for search using “Adobe Community Help. I had no prior experience with Flex and AIR. My first attempt ended as a simple project for college, a spaghetti code. All examples found on the web or official documentation are based on massive use mxml code. I ended up having a glorious mxml file. Huge :-).

Some smart guys are telling you to split your mxml using an ActionScript file but is like putting the trash under the carpet. It is still spaghetti code but in two dishes.

I tried to remember how I would have programmed it in Java Swing.  9 years have passed since then but I remembered. TableModel, DefaultListModel, ButtonModel etc. they all came out from a dark corner of my memory. Yes ! I remembered, Swing MVC.

A typical sample from documentation…..

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
   3:     <mx:Panel x="10" y="10" width="350" height="200" title="Rate Customer Service">
   4:         <mx:ComboBox id="cbxRating" x="20" y="20" width="100">
   5:             <mx:dataProvider>
   6:                 <mx:Array>
   7:                     <mx:String>Satisfied</mx:String>
   8:                     <mx:String>Neutral</mx:String>
   9:                     <mx:String>Dissatisfied</mx:String>
  10:                 </mx:Array>
  11:             </mx:dataProvider>
  12:         </mx:ComboBox>
  13:         <mx:Button x="140" y="20" label="Send"/>
  14:     </mx:Panel>
  15: </mx:Application>

Who hard-codes his application in this way? In a typical application you get this stuff from files or database. But let’s see the good part, at least is giving you a hint, a ComboBox and List are accepting an Array as input.

MVC applied to Flex

First of all a briefing about MVC

  1. Model, is the data. Manipulates the internal state and fire events when the internal state changed.
  2. View, the visual representation for the Model’s data (controls on the screen)
  3. Controller, is responsible for interpreting the user actions on the view and make changes to the model. (usually an event handler in flex)

In reality there is no 100% demarcation between these three layers. Is not that easy to make them completely decoupled and usually we end up making some tradeoffs.

The controller will always know about the view and the view about the controller. Controller also knows about the model. In the end I could say that the model is the only piece of the MVC that can be “100% decoupled”.

Let’s try to put a simple screen like this into MVC.


We fill in some text and then press Add. After we press add the text box is cleared and the text is added into de the list. In the end wee look into it and try to make a reusable component.


The model contains the actual data, and for our example it should a class that hold list values. As I pointed out earlier we could use an ArrayCollection to store the list. For this we make an actionscript class named ListModel

The view should display model’s changes and for this we mark the model class as [Bindable]. This is nice feature of ActionScript, no such thing in Java Swing.

Now, to add a new item in the list we simply nee an addElement function. In this example we make the model a singleton. This means that if we make more than one list they will share the same data.

   1: package org.bserban.flex.simplemvc.model
   2: {
   3:     import mx.collections.ArrayCollection;
   4:     /**
   5:     * Model class used to store list data and to
   6:     * provide access to the list content.
   7:     *
   8:     * For demo purposes, we have only addElement.
   9:      */
  10:     [Bindable]
  11:     public class ListModel
  12:     {
  13:         public var items :ArrayCollection = new ArrayCollection();
  14:         private static var instance:ListModel;
  15:         public function ListModel()
  16:         {
  17:             instance=this;
  18:         }
  19:         /**
  20:          * For this demo we treat this class as singletone.
  21:          * If list is reused the component that reuse it
  22:          * must keep reference to model and controler.
  23:          */
  24:         public static function getInstance():ListModel{
  25:             if(instance == null){
  26:                 instance = new ListModel();
  27:             }
  28:             return instance;
  29:         }
  30:         /**
  31:          * Add a new element to the list, the model is bindable,
  32:          * no further actions are required, the view
  33:          * is notified automatically.
  34:          */
  35:         public function addElement(itm:String): void{
  36:             items.addItem(itm);
  37:             trace(" item added, now we have:"+items.length);
  38:         }
  39:     }
  40: }


The controller is responsible for the interaction between view and model. In our case will make the validation of the text, it doesn’t allow the view to add empty text into the list. Let’s make a new class named Controller. As an exercise add also a sort function.

The controller holds a reference to the model and provides functions to the view. In a more advanced implementation the Controller would listen for events from the view and the decides what action should do.

   1: package org.bserban.flex.simplemvc.controller
   2: {
   3:     import org.bserban.flex.simplemvc.model.ListModel
   4:     import mx.utils.StringUtil;
   5:     public class Controller
   6:     {
   7:        private var model:ListModel = ListModel.getInstance();
   8:         /**
   9:          * Glue the interaction between view and model.
  10:          */
  11:         public function Controller(){
  12:         }
  13:         /**
  14:          * add a new item into the list model.
  15:          */
  16:         public function addPerson(name:String):void
  17:         {
  18:             if(name==null || StringUtil.trim(name).length==0){
  19:                 trace("empty name");
  20:             }
  21:             else{
  22:                 model.addElement(name);
  23:            }
  24:         }
  25:     }
  26: }


The view is the graphical representation of the component. Usually it is a an mxml file but it can be also an actions script file for advanced programmers. It binds his data to the model and use the controller to process the view events. Let’s name it SimpleAirMVC.mxml. I added Air to its name because i decided to make an air project in Flex Builder.

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="226" width="235">
   3:     <mx:Script>
   4:         <![CDATA[
   5:         import mx.controls.List;
   6:         import org.bserban.flex.simplemvc.model.ListModel;
   7:         import org.bserban.flex.simplemvc.controller.Controller;
   9:         [Bindable]
  10:          private var model:ListModel = ListModel.getInstance();
  11:         private function clickHandler():void
  12:             {
  13:                 var controler:Controller = new Controller();
  14:                 controler.addPerson(personTxt.text);
  15:                 personTxt.text="";
  16:             }
  17:              ]]>
  18:     </mx:Script>
  20:     <mx:TextInput x="10" y="10" id="personTxt" text="" width="145"/>
  21:     <mx:Button x="163" y="10" label="Add" id="btnAdd" click="clickHandler()"/>
  22:     <mx:List x="10" y="40" width="218" dataProvider="{model.items}"></mx:List>
  23: </mx:WindowedApplication>

This a  simple way to implement a MVC in a small application. Things can become more complicated if the application is big. If this is the case then you should look over Pure MVC framework or Cairngorm. They eliminate the dependencies between MVC layer by using events. It is an event driven approach.

You may download the source code here SimpleAirMVC

Further reading

I recommend you to read the books ActionScript 3.0 Design Patterns and Advanced ActionScript 3 with Design Patterns