1

In my web application, there are quite a few entities, and most of them require CRUD operations. So I am thinking about writing a generic DAO that can handle CRUD for all of the entities. I've found a tutorial article from IBM, but don't quite understand the generic implementation using the generic type 'T', and 'PK'. The article is at this link

I wrote the following DAO by using Object type in all the methods, and they seem working just fine - all my entities are able to do CRUD with the following CommonDao class. Although it works for my needs, I'm looking for what is the best practice for implementing a generic DAO class for Hibernate.

public class CommonDao
{
    private final static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    public CommonDao() {}

    @UnitOfWork
    public List findAll(Object className)
    {
        List types = null;

        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(className + ".class");
        types = (List <Object>) criteria.list();
        session.close();

        return types;
    }

    @Transactional
    public void saveObject(Object obj)
    {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.saveOrUpdate(obj);

        tx.commit();
        session.close();
    }

    @Transactional
    public void saveObjectWithManyEntities(Object obj, Set<Object> objects)   /* for OneToMany relationships */
    {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.saveOrUpdate(obj);

        for (Object o : objects)
        {
            session.save(o);
        }

        tx.commit();
        session.close();
    }
}
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
TonyW
  • 18,375
  • 42
  • 110
  • 183
  • 2
    The seemingly random use of `save` vs `saveOrUpdate` is not doing you any favours. Without generics, you are also resorting to casting, which introduces runtime type errors. I would seriously consider rewriting this class in a generic way - and if possible using JPA criteria queries. `List` - i.e. the `List` rawtype should never been seen in modern Java. – Boris the Spider Jun 25 '14 at 16:46
  • There is a very similar discussion here...http://stackoverflow.com/questions/3573479/how-to-create-a-generic-dao-class-using-hibernate-context-sessions?rq=1 – Nilesh Jun 26 '14 at 08:11

1 Answers1

2

The best way to do it is to include Spring Data into your project. The JPA repositories offer basic CRUD, pagination, sorting and most of your queries could be built automatically from the methods naming convention.

Before Spring Data we'd resort to such Generic Daos but not anymore.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Good suggestion. @TonyGW - you may find this answer on Spring Data helpful http://stackoverflow.com/questions/9721383/hibernate-crud-generic-dao?rq=1 – Nilesh Jun 26 '14 at 08:14