// Session cache: Level 1 Cache Public class HibernateDemo3 { // Verify the existence of the cache @ Test Public void testaddStudent (){ Session session = HibernateUtil. getSession (); Transaction tx = session. beginTransaction (); Person p1 = (Person) session. get (Person. class, 1); // same as OID Person p2 = (Person) session. get (Person. class, 1); // same as OID. Because this object exists in the cache, it is directly retrieved and will not be queried in the database again. System. out. println (p1 = p2); // true Tx. commit (); Session. close (); } // Manage the first-level cache /* * Session. flush (): refresh the cache (synchronize the database ). The cached data is still * Session. clear (): clears the cache. * Session. evict (): clears the specified object. */ @ Test Public void testSessionManager1 (){ Session session = HibernateUtil. getSession (); Transaction tx = session. beginTransaction (); Person p1 = (Person) session. get (Person. class, 1); // get the Persistent Object and put it in the Session cache. System. out. println (p1 ); // Session. flush (); // only updates the database. The data in the cache is still // Session. clear (); // clear the cache Session. evict (p1); // clear the specified object from the cache Person p2 = (Person) session. get (Person. class, 1 ); System. out. println (p2 ); Tx. commit (); Session. close (); } // Snapshot: // Refresh when commit is enabled @ Test Public void testSessionManager2 (){ Session session = HibernateUtil. getSession (); Transaction tx = session. beginTransaction (); Person p1 = (Person) session. get (Person. class, 1); // get the Persistent Object and put it in the Session cache. P1.setName ("Chen xingyu 2"); // refresh database data can only refresh persistent child entities Tx. commit (); // synchronize the database at this time Session. close (); } @ Test // Manual flush will refresh Public void testSessionManager3 (){ Session session = HibernateUtil. getSession (); Transaction tx = session. beginTransaction (); Person p1 = (Person) session. get (Person. class, 1); // get the Persistent Object and put it in the Session cache. P1.setName ("Chen xingyu"); // refresh database data can only refresh persistent child entities Session. flush (); // synchronize the database Tx. commit (); // synchronize the database at this time Session. close (); } @ Test // During Query execution, the changed object classes are automatically synchronized to the database. Public void testSessionManager4 (){ Session session = HibernateUtil. getSession (); Transaction tx = session. beginTransaction (); Person p1 = (Person) session. get (Person. class, 1); // get the Persistent Object and put it in the Session cache. P1.setName ("Chen xingyu 1"); // refresh database data can only refresh persistent State entities Query q = session. createQuery ("from Person"); // a mandatory SQL statement System. out. println (q. list (). get (0 )); Session. flush (); // synchronize the database Tx. commit (); // synchronize the database at this time Session. close (); } @ Test // Verify the refresh Mode Public void testSessionManager5 (){ Session session = HibernateUtil. getSession (); Transaction tx = session. beginTransaction (); // Session. setFlushMode (FlushMode. COMMIT); // when the transaction is committed and manually flush. Not flushed during Query // Session. setFlushMode (FlushMode. MANUAL); // when the transaction is committed and manually flush. Not flushed during Query Person p1 = (Person) session. get (Person. class, 1); // get the Persistent Object and put it in the Session cache. P1.setName ("Chen xingyu"); // refresh database data can only refresh persistent child entities Query q = session. createQuery ("from Person"); // a mandatory SQL statement System. out. println (q. list (). get (0); // get from Cache Tx. commit (); // synchronize the database at this time Session. flush (); // synchronize the database Session. close (); } // Refresh: overwrites the cached data with the data in the database. @ Test Public void testSessionManager6 (){ Session session = HibernateUtil. getSession (); Transaction tx = session. beginTransaction (); Person p1 = (Person) session. get (Person. class, 1); // get the Persistent Object and put it in the Session cache. P1.setName ("Chen xingyu "); Session. refresh (p1); // overwrite the cached data with the data in the database Tx. commit (); // synchronize the database at this time Session. close (); } } |