What is the difference between getHibernateTemplate().flush() and getSession().flush()

hibernate, java, spring

Solution

HibernateTemplate is deprecated Spring code, from the days before Spring moved to annotations as the preferred method for transaction management. It is not a part of Hibernate itself. The template code abstracted away the mechanics of creating, committing, and rolling back transactions, allowing the developer to focus solely on their business logic. HibernateTemplate is now considered superfluous by the Spring community, and has been completely removed in Spring's support for Hibernate 4.

From the official documentation:

NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate3 style of coding data access objects instead, based on SessionFactory.getCurrentSession().

Your code is likely a mishmash of legacy code and mixed approaches amongst developers during the transition.

Problem

I am using Hibernate 3.2.6. And I am facing exception save the transient instance before flushing In my code sometimes we are using `getSession().flush()` and sometime we are using `getHibernateTemplate().flush()` in one transaction. Could you please tell me what is the difference between those two?

Original source

Related problems