Hibernate learning: querying Cache

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.