Hibernate learning: querying Cache
I. query cache configurations
(1) Open the query cache in the Hibernate configuration file
True
Note: The query cache depends on the second-level cache. Enable the second-level cache first.
(2) query. setCacheable (true)
List List = (List ) Session. createQuery ("from User"). setCacheable (true). list ();
List List2 = (List ) Session. createQuery ("from User"). setCacheable (true). list ();
Ii. Unit Test
@org.junit.Test@SuppressWarnings({ "unused", "unchecked" })public void testQueryCahce(){Session session = factory.openSession();session.beginTransaction();List
list = (List
)session.createQuery("from User").setCacheable(true).list();List
list2 = (List
)session.createQuery("from User").setCacheable(true).list();session.getTransaction().commit();session.close();}
Output result: Only one SQL statement is output, proving that the query cache has taken effect!
Hibernate:
Select
User0 _. id as id0 _,
User0 _. group_id as group3_0 _,
User0 _. name as name0 _
From
User user0 _
AfterClass