Five ‘s Weblog

October 13, 2007

DAO pattern for Hibernate!

Filed under: Programming — by powerdream5 @ 3:20 am
Tags: , ,

        In J2EE field, open source framework is pretty hot. One of the advantages of them is that they are non-intrusive, because  you don’t need to make your classes extend any classes or implement any interfaces from the framework. However, when we use the ORM framework, such as Hibernate or JDO, we still need to use some specific classes of them. Therefore, in order to make the application more extensible, we should encapsulate the persistence layer of the system.

        DAO(data access object) pattern has already been proved to be a good way to seprate the business logic layer and the persistent layer. Today, I want to show an example of applying DAO pattern to Hibernate.

       Usually, a typical DAO pattern contains a factory class, some DAO interfaces and some DAO classes which implement those interface. Now, Let’s take a blog system for an example. Because I just want to show the structure, I will not present the detail of each class.

10_11_1_2007.gif

      Firstly, we define the factory class:

public class DAOFactory{
        private static DAOFactory daoFactory;
        static{
               daoFactory = new DAOFactory();
        }
        //Define the factory method, return the instance of specific classes
        public DAOFactory getInstance(){
               return daoFactory;
        }
        public BlogDAO getBlogDAO(){
               return new BlogDAOImpl();
        }
         public BlogDAO getCommentDAO(){
               return new CommentDAOImpl();
        }
}

       Secondly, we define the DAO interfaces. The purpose of interfaces is to make our applicaiton more extensible. For example, if we want to change the hibernate framework to the JDO framework in the future, we don’t need to make any change to the business logic, just changing the implementation of the interface works well.

//Data access object interface 
public interface BlogDAO{
      //Blog are an persistence class
      //define the method for inserting a blog to the system
      public boolean insertBlog(Blog blog);
     //define the method to read a blog from the system according the id
          public Blog readBlog(Long blogId);
}

      Then, make an implementation to this interface:

//Data access object 
public class BlogDAOImpl implements BlogDAO{
      public boolean insertBlog(Blog blog){
             Session session = HibernateUtil.getSession();
             ….
      }
     public Blog readBlog(Long blogId){
             Session session = HibernateUtil.getSession();
             …. 
      }
}

//HibernateUtil is a help class
public class HibernateUtil{
        private static SessionFactory sessionFactory;
        static{
                sessionFactory = ….;
         }
        public static Session getSession(){
               return sessionFactory.openSession();
        }
}

      Now, we can create a class for testing:
public class Test{
     public static void main(String[] args){
             //insert a blog to the database
            //assume that we already get an instance of Blog class named blog

            BlogDAO blogDAO = DAOFactory.getInstance().getBlogDAO();
            blogDAO.inserBlog(blog);
     }
}

      However, there are still some drawbacks of DAO pattern. For example, we always have to get an instance of Session at the beginning of each method of the class BlogDAOImpl, which make us write the same code. Fortunately, we can make some advances to this program, for instance, we can adopt spring framework for configuring the relationship between each class.

10_11_1_3.jpeg

Blog at WordPress.com.