Hi, I've a general question regarding DAO design. In this case I'm designing DAO basing on Hibernate API, but I suppose same goes for JPA.
My question is following: the Session.get() method returns persistant object reference, meaning that any updates on the object will be reflected to database. My assumption was this should be a role of DAO to persist modified objects (thus he saveOrUpdate) method.Code:public class UserDaoImpl { private final SessionFactory sessionFactory; public UserDaoImpl(SessionFactory sessionFactory) { super(); this.sessionFactory = sessionFactory; } public void saveOrUpdate(User user) { sessionFactory.getCurrentSession().saveOrUpdate(user); } public User getById(Integer id) { return (User) sessionFactory.openSession().get(User.class, id); } ... }
In my overall application design I was thinking of passing the domain objects (such as User etc) right down to view part, where they would be modified, and than returned back to business layer and persisted eventually with DAO. Needless say, I don't wan't any implicit database updates upon domain object modification, only explicit save/updates.
I'd really appreciate opinions about this. Am I taking the right approach in this case?
Second question is does it make make sense to use Session.getCurrentSession for queries only? In spring, service calling such dao function would need to be transactional, does that make sense for ueries only


Reply With Quote