HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaspringMinor

Create better Base DAO class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
createdaobetterclassbase

Problem

I created a DAO class which is base class for all other DAO classes.

I use Spring Framework 4 and Hibernate 4.

Question: Is there anything that could be done better?

public class GenericDaoImpl implements GenericDao {
    private Class entityClass;

    @Autowired
    private SessionFactory sessionFactory;

    public GenericDaoImpl(Class entityClass) {
        this.entityClass = entityClass;
    }

    @Override
    public Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List findAll() throws DataAccessException {
        return getCurrentSession().createCriteria(entityClass).list();
    }

    @Override
    @SuppressWarnings("unchecked")
    public E find(I id) {
        return (E) getCurrentSession().get(entityClass, id);
    }

    @Override
    public void create(E e) {
        getCurrentSession().save(e);
    }

    @Override
    public void update(E e)  {
        getCurrentSession().update(e);
    }

    @Override
    public void delete(E e) {
        getCurrentSession().delete(e);
    }

    @Override
    public void flush() {
        getCurrentSession().flush();
    }
}


Sample DAO class which extends base DAO class is for example this one:

@Repository("articleDao")
public class ArticleDaoImpl extends GenericDaoImpl implements ArticleDao {
    public ArticleDaoImpl(){
        super(ArticleEntity.class);
    }
}

Solution

To be able to create a parameter-less constructor add the following code into your constructor. It will use reflection to set entityClass. This way you don't even need to worry about passing in a class type, you can extend out your generic DAO and its type will be set by parameterisation.

public GenericDaoImpl() {
    Type e = getClass().getGenericSuperclass();
    ParameterizedType pt = (ParameterizedType) e;
    entityClass = (Class) pt.getActualTypeArguments()[0];
}


This removes the need for having constructors in subclasses of your GenericDAOImpl.

Why don't you combine your create and update methods? It makes for easier service calls.

@Override
public void saveOrUpdate(E e){
    getCurrentSession().saveOrUpdate(e);
}


Hibernate will automatically make the determination whether a save or update call is appropriate. If your ID field is set, then perform an update, otherwise perform a save.

Also, I would remove getCurrentSession() from your interface and change the method to private. You shouldn't be accessing the Hibernate session from anywhere outside the DAO so there is no reason to expose out your getCurrentSession() method.

Code Snippets

public GenericDaoImpl() {
    Type e = getClass().getGenericSuperclass();
    ParameterizedType pt = (ParameterizedType) e;
    entityClass = (Class<E>) pt.getActualTypeArguments()[0];
}
@Override
public void saveOrUpdate(E e){
    getCurrentSession().saveOrUpdate(e);
}

Context

StackExchange Code Review Q#48603, answer score: 2

Revisions (0)

No revisions yet.