In Hibernate in Action, it recommends to create a DAO object and when I want to save a persistent object, it should be saved like this.
However, isn't it better to have just 1 save method in HibernateUtil where it handles all the begin/close/commit transaction?
What is the drawback of that approach?
from:
Code:
UserDAO userDAO = new UserDAO();
User u1 = new User("Christian", "Bauer", "turin", "abc123", "christian@hibernate.org");
userDAO.makePersistent(u1);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
to:
Code:
HibernateUtil.save(u1);
// where HibernateUtil.save() is this:
HibernateUtil.save(User u1) {
HibernateUtil.beginTransaction();
HibernateUtil.getSession().saveOrUpdate(u1);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
}