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

Advertisements

9 Comments »

  1. If u adopt spring framework for configuring the relationship between the
    business logic class and the dao,u would like to take use of spring’s orm-support for hibernate.

    Comment by M.Liang Liu — October 13, 2007 @ 8:23 am |Reply

  2. you r more beautiful than the factory pattern…..

    Comment by vaibhav — March 26, 2008 @ 9:42 am |Reply

  3. i got more knowledge and exact point by looking this code.

    Comment by utkal samal — December 21, 2008 @ 10:36 pm |Reply

  4. The information what u hav provided is good but i need still more information regarding caching..Can u provide it

    Comment by Lathashree — April 28, 2009 @ 2:48 am |Reply

  5. I think with use of spring even the DAOFactory can be a spring bean that takes references to all other DAOs

    Comment by adnan — January 7, 2010 @ 11:25 am |Reply

  6. just for follow-up 🙂

    Comment by adnan — January 7, 2010 @ 11:27 am |Reply

  7. Good one.

    Comment by R mishra — February 5, 2010 @ 8:14 am |Reply

  8. Nice little article.
    I have developed a persistence code generator . Which generates DAO classes with default functionality (All are static methods).
    I using hibernate framework.
    My question is that good practice to have DAO with static methods?

    Comment by Husmukh — April 3, 2010 @ 2:45 am |Reply

  9. Do you mind if I quote a couple of your articles as long as
    I provide credit and sources back to your website?

    My blog is in the exact same area of interest as yours and my visitors would really benefit from a lot of the information
    you present here. Please let me know if this alright with you.
    Appreciate it!

    Comment by farming — July 7, 2013 @ 11:59 pm |Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: