First, the session in the Flushmode settings:
Set the Flushmode property before the transaction is opened, Method Session.setflushmode (flushmode.always| auto| Commit| never| MANUAL).
Flushmode has 5 values to choose from: Always: Any code will flush, auto: Default mode – Auto, Commit:commit, never: Never, MANUAL: Manual mode.
Second, the session flush () method Description:
You can force synchronization from memory to the database, Method Session.flush ().
Cases:
@Test /** * Flush enforces synchronization with database * /void Testflush () {Session session = Hibernateuitl.getsessionfactory (). Getcurrentsession (); Session.begintransaction (); Teacher t = (Teacher) session.get (Teacher. class, 3); T.setname ("yyy"); T.setname ("yyyyy" )
Looking at this code, we SetName () 2 times, but the program only changes the database once, at commit time.
@Test /** * Flush enforces synchronization with database * /void Testflush () {Session session = Hibernateuitl.getsessionfactory (). Getcurrentsession (); Session.begintransaction (); Teacher t = (Teacher) session.get (Teacher. class, 3); T.setname ("yyy"); Session.flush (); there is a flush that executes 2 times Updae and does not execute only once T.setname (" yyyyy"
We performed Session.flush () at the 2nd time SetName ().
And look at the SQL statements executed by hibernate.
Hibernate: update Teacher set birthday=?, name=?, title=? where id=? Hibernate: update Teacher set birthday=?, name=?, Title=? where id=?
Performed 2 Update
So it looks like the Flush method forces the database to be synchronized.
The Flush method can be set, that is, when fulsh execution can be set (the first one has a description).
Second, the session clear () method Description:
Either load or Get will first look for the cache (primary cache) if not, it will go to the database lookup, call the Clear () method, you can force clear session cache.
Cases:
Public void testclear () { = hibernateuitl.getsessionfactory (). Getcurrentsession (); Session.begintransaction (); = (Teacher) session.get (Teacher. Class, 3); System.out.println (T.getname ()); = (Teacher) session.get (Teacher. Class, 3); System.out.println (T2.getname ()); Session.gettransaction (). commit (); }
The 2 Get method is used here (the Get method executes the SQL statement immediately), but because the first execution caches an entity with ID 3, there are 2 get methods that execute only one SQL statement at a time.
Public void testclear () { = hibernateuitl.getsessionfactory (). Getcurrentsession (); Session.begintransaction (); = (Teacher) session.get (Teacher. Class, 3); System.out.println (T.getname ()); Session.clear (); // it's not clear. Only executes SQL statements once, with clear executing 2 times Teacher t2 = (Teacher) session.get (Teacher. Class, 3); System.out.println (T2.getname ()); Session.gettransaction (). commit (); }
Here we execute session.clear () before the 2nd get, and we put hibernate show_sql out, and it executes 2 SQL statements.
So Session.clear () clears the cache.
Hibernate force clears session cache clear and Flush method Flushmode settings