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<E, I extends Serializable> implements GenericDao<E, I> {
private Class<E> entityClass;
@Autowired
private SessionFactory sessionFactory;
public GenericDaoImpl(Class<E> entityClass) {
this.entityClass = entityClass;
}
@Override
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
@Override
@SuppressWarnings("unchecked")
public List<E> 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<ArticleEntity, Long> implements ArticleDao {
public ArticleDaoImpl(){
super(ArticleEntity.class);
}
}
getCurrentSession
togetSession
which is clear enough. – David Harkness May 3 '14 at 18:17sessionFactory
. I don't think that it is an improvement. – user41403 May 7 '14 at 18:50