5
\$\begingroup\$

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);
 }
}
asked Apr 30, 2014 at 18:12
\$\endgroup\$
3
  • \$\begingroup\$ I would shorten getCurrentSession to getSession which is clear enough. \$\endgroup\$ Commented May 3, 2014 at 18:17
  • \$\begingroup\$ @DavidHarkness It is the same as original name from sessionFactory. I don't think that it is an improvement. \$\endgroup\$ Commented May 7, 2014 at 18:50
  • \$\begingroup\$ Where is the Uses for I \$\endgroup\$ Commented Sep 1, 2016 at 15:00

1 Answer 1

2
\$\begingroup\$

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<E>) 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.

answered May 16, 2014 at 4:50
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.