Data Access Object pattern is a widely used pattern and is essential part of modern java based web applications.In this tutorial we will see an implementation using Hibernate to communicate with the database.

First step will be to create AbstracDao class that will be extended by all Data Access Objects.AbstractDao class contain all that methods that are common to all Data Objects.They also contain some configuration parameters such as setting the session etc.


package com.javaonly.Daos;

import java.util.List;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

public class AbstractDao {

    private Session sez;
    private Class clz;
    protected static final Logger systemLogger = Logger.getLogger("system");

    /*a new instance of AbstractDAO */
    public AbstractDao() {
    }

    public AbstractDao(Class clz) {
        this.clz = clz;
    }

    public Session getSez() {
        return sez;
    }

    public void setSez(Session sez) {
        this.sez = sez;
    }

    public Class getClz() {
        return clz;
    }

    public void setCla(Class clz) {
        this.clz = clz;
    }

    public List getAll() {
        Query q = this.sez.createQuery("FROM " + cla.getName());
        return q.list();
    }

    public void save(Object o) {
        sez.save(o);
    }

    public void delete(Object o) {
        sez.delete(o);
    }

    public void rollback(Object o) {
        sez.evict(o);
    }

    public Object getById(Integer id) {
        return sez.get(cla, id);
    }
}

Now we have all we need in order to create a full functional Data Access Object.As an example we  will create a tutorialDAO that we use for communicating with our tutorial table in the database


package com.javaonly.Daos;

import com.javaonly.model.Tutorial;
import java.util.List;
import org.hibernate.Query;


public class TutorialDao extends AbstractDao {

    public TutorialDao() {
        super(Tutorial.class);
    }

   

    public List getPopularTutorials() {
        Query q = this.getSez().createQuery("FROM  Tutorial where possitiveVotes>negativeVotes ORDER BY possitiveVotes DESC").setMaxResults(5);
        return q.list();
    }

    

    public List getTutorialsByCategory(int id) {
        Query q = this.getSez().createQuery("FROM  Tutorial where category.id=" + id);
        return q.list();
    }
}

As you can see we 've added some  tutorial-specific methods and  queries but common methods such as getAll() or getById() are inherited from AbstractDAO

As a final step we need a Factory class that we will use to instantiate and configure our Data Access Objects. Below you can see the DAOFactory class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javaonly.Daos;

import com.javaonly.hibernate.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author master
 */
public class DaoFactory {
    //returns categoryDAO
    public static CategoryDao getCategoryDao()
    {
        return (CategoryDao)getDAOByClass(CategoryDao.class);
    }
    
    //returns userDAO
    public static UserDao getUserDao()
    {
        return (UserDao)getDAOByClass(UserDao.class);
    }
    //returns tutorialDAO
    public static TutorialDao getTutorialDao()
    {
        return (TutorialDao)getDAOByClass(TutorialDao.class);
    }
    
    //returns the DAO and configure's it with current session
    
    public static AbstractDao getDAOByClass(Class c)
    {
        try 
        {
            Session s = getCurrentSession();
            AbstractDao d = (AbstractDao)c.newInstance();
            d.setSez(s);        
            return d;
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    public static Session getCurrentSession()
    {
        return HibernateUtil.getSessionFactory().getCurrentSession();
    }
}