Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 667
Srinivasan Avatar asked Feb 19 '14 11:02

Srinivasan


People also ask

Why do we use flush in hibernate?

Flushing the session forces Hibernate to synchronize the in-memory state of the Session with the database (i.e. to write changes to the database). By default, Hibernate will flush changes automatically for you: before some query executions. when a transaction is committed.

Does session flush commit transaction?

flush() will synchronize your database with the current state of object/objects held in the memory but it does not commit the transaction. So, if you get any exception after flush() is called, then the transaction will be rolled back.


1 Answers

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.

like image 132
Peter Bratton Avatar answered Oct 18 '22 16:10

Peter Bratton