I'm new to Hibernate and have some very basic questions.
In the following piece of code...
Quote:
public Object hibernateMerge(Object object) throws DataAccessException {
ManagedSessionContext.bind(HibernateUtil.getSingletonSession());
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Object retval = HibernateUtil.getSessionFactory().getCurrentSession().merge(object);
HibernateUtil.getSessionFactory().getCurrentSession().flush();
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
ManagedSessionContext.unbind(HibernateUtil.getSessionFactory());
return retval;
}
...two questions:
(1) What does the flush() achieve right before the commit()? It is my understanding that the commit() also performs a flush() - so isn't the explicit flush() call redundant? Or worse, might cause any problems?
(2) It is obvious that
HibernateUtil.getSessionFactory().getCurrentSession() in line 2 returns the same Session object as does
HibernateUtil.getSingletonSession() in line 1. As such, is the
ManagedSessionContext.bind necessary in this code?
In other words, can I replace the above code with the following?
Quote:
public Object hibernateMerge(Object object) throws DataAccessException {
Session session = HibernateUtil.getSingletonSession();
session.beginTransaction();
Object retval = session.merge(object);
session.getTransaction().commit();
return retval;
}
Thanks.