Day37 07-hibernate Level Two cache: query cache

Source: Internet
Author: User

The query cache is a more powerful cache than the two-level cache. You must configure the level two cache before you can use the query cache, otherwise it will not be used. Level two caches are primarily cache/object caches of classes. The query cache is also available for objects (because the feature is more powerful than a level two cache), and it can also target properties in a class.

Select CNAME from Customer. This level two cache is not cached. The two-level cache can only cache the entire object. The object's properties are cached in our query cache. This is the maximum difference between a query cache and a two-level cache.

@Test//testing for Query caching     Public voidDemo9 () {Session session=hibernateutils.getcurrentsession (); Transaction TX=session.begintransaction (); //The from customer then. List () has proved to be able to store data at level two, which is meaningless.Query query = Session.createquery ("Select C.cname from Customer C");//The properties of the object are cached. Level two cache cannot cache properties of an object.//querying some fields must use the query cache to improve your efficiency. //use Query caching:Query.setcacheable (true); Query.list ();//If you query.list directly (), it is not cached.Tx.commit (); Session=hibernateutils.getcurrentsession (); TX=session.begintransaction (); Query= Session.createquery ("Select C.cname from Customer C"); Query.setcacheable (true);                Query.list ();    Tx.commit (); }
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hibernate. Org/dtd/hibernate-configuration-3.0.dtd "><hibernate-configuration><session-factory>    <!--properties that have to be configured -    <!--to configure basic information for a database connection: -    < Propertyname= "Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</ Property>    < Propertyname= "Hibernate.connection.url">jdbc:mysql:///hibernate3_day03</ Property>    < Propertyname= "Hibernate.connection.username">Root</ Property>    < Propertyname= "Hibernate.connection.password"></ Property>    <!--the dialect of Hibernate -    <!--generate the underlying SQL different -    < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</ Property>    <!--Optional Properties -    <!--Show SQL -    < Propertyname= "Hibernate.show_sql">True</ Property>    <!--Formatting SQL -    < Propertyname= "Hibernate.format_sql">True</ Property>    < Propertyname= "Hibernate.connection.autocommit">False</ Property>    <!--hbm: Map to ddl:create drop alter -    < Propertyname= "Hibernate.hbm2ddl.auto">Update</ Property>    <!--to set the isolation level of a transaction -    < Propertyname= "Hibernate.connection.isolation">4</ Property>    <!--Set Local session -    < Propertyname= "Hibernate.current_session_context_class">Thread</ Property>    <!--c3p0 Connection Pool settings -    <!--to configure a vendor for a connection pool using the C3PO connection pool -    < Propertyname= "Connection.provider_class">Org.hibernate.connection.C3P0ConnectionProvider</ Property>    <!--two level cache is turned on in hibernate -    < Propertyname= "Hibernate.cache.use_second_level_cache">True</ Property>    <!--Configure a provider for level two cache -    < Propertyname= "Hibernate.cache.provider_class">Org.hibernate.cache.EhCacheProvider</ Property>    <!--Configure query Caching -    < Propertyname= "Hibernate.cache.use_query_cache">True</ Property>    <!--minimum number of database connections available in the connection pool -    < Propertyname= "C3p0.min_size">5</ Property>    <!--maximum number of all database connections in the connection pool -    < Propertyname= "C3p0.max_size">20</ Property>    <!--sets the expiration time of the database connection, in seconds, that is purged from the connection pool if a database connection in the connection pool is idle for more than the timeout time -    < Propertyname= "C3p0.timeout">120</ Property>    <!--Check for idle connections in all connection pools in seconds every 3,000 seconds -    < Propertyname= "C3p0.idle_test_period">3000</ Property>    <!--notifies hibernate to load those mapping files -    <MappingResource= "Cn/itcast/hibernate3/demo1/customer.hbm.xml" />    <MappingResource= "Cn/itcast/hibernate3/demo1/order.hbm.xml" />    <!--Configure which classes use level two caching -    <Class-cacheusage= "Read-write"class= "Cn.itcast.hibernate3.demo1.Customer"/>    <!--class buffers for querying orders -          <Class-cacheusage= "Read-write"class= "Cn.itcast.hibernate3.demo1.Order"/>        <!--Collection Buffers So when we get the number of orders from the customer, we don't send the SQL statements anymore. Because it has already cached our collection orders.    Collection buffers are used to cache collections in objects. We are now using the collection in the object, so it does not send the SQL statement.    The data of the set buffer is proved to be dependent on the class buffer.  -    <Collection-cacheusage= "Read-write"Collection= "Cn.itcast.hibernate3.demo1.Customer.orders"/></session-factory></hibernate-configuration>

    <!---    <name = "Hibernate.cache.use_ Query_cache ">true</Property>

Day37 07-hibernate Level Two cache: query cache

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.