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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With