Himai Minh wrote:Hi,
I don't know much details about the code. But I believe Hibernate will automatically commit the changes to the DB after a transaction is completed successfully.
But we may also use Session.flush() to make sure changes are committed.
Reference about flush:
https://stackoverflow.com/questions/3220336/whats-the-use-of-session-flush-in-hibernate
It is important to realize that Hibernate operates in two flavours:
1. Legacy (proprietary) Hibernate, which is based on Session
2. Hibernate JPA, which implements the
JEE Standard
Java Persistence Architecture components of the Enterprise JavaBean subsystem. JPA uses an EntityManager.
I recommend using Hibernate JPA, not legacy Hibernate. Legacy Hibernate may go away some day and probably will not be as well maintained as Hibernate JPA. Plus JPA is a portable standard API. I have had occasion to switch between Apache OpenJPA and Hibernate JPA in order to gain features and/or dodge bugs and the switch between the two standards implementation is a lot simpler than switching in and out of a proprietary interface.
The flush() method pushes the contents of the Hibernate buffers out to the database server. JPA EntityManager flush() and proprietary Hibernate Session flush() are pretty much identical on that. But that doesn't mean that fetching data from the database will return the flushed-out data. Here you have to distinguish between local Transaction and database transaction contexts. If the local Spring Transaction context being used initiates a database Transaction context, then only when the database Transaction is committed will any other database reading processes see the changed. If Spring initiates a database Transaction automatically then it will automatically do a database Transactiont commit when the Spring framework Transaction is committed.
Spring does offer several different types of Transaction contexts, however, and some of them may allow multiple transactions/commits to be done within a single Spring Transaction. It all depends on how you set things up.