Hibernate's first level cache and level two cache are caches of entities, and he does not cache normal attributes, and if you want to cache normal familiarity, consider using query caching.
For the query cache, the key that he caches is the HQL or SQL statement used by the query, the query cache not only requires the same statement, but also requires that the parameters must be consistent, the query cache is closed by default, in order to open the query cache, the Hibernate.cfg.xml configuration file needs to be modified. Add the following configuration:
<property name= "Hibernate.cache.use_query_cache" >true</property>
To use the query cache, you must also use the Set Cacheable method to turn the cache on when the query is called ,as shown in the example code:
??
public static void Main (string[] args) {
Session cursession = Hibernateutils.opensession ();
try {
??
First query, Cache enabled
List List = Cursession.createquery ("Select TRM from Transitrecordmodel TRM"). Setcacheable (True). List ();
System.out.println ("Query size=" + list.size ());????????????????
Second query, cache enabled, read from cache, do not initiate database query operation
List = Cursession.createquery ("Select TRM from Transitrecordmodel TRM"). Setcacheable (True). List ();
System.out.println ("Query size=" + list.size ());
} finally {
if (cursession! = null) {
Cursession.close ();
}
}
}
??
interaction between the query cache and the level two cache There are several types of settings that are set by the set CacheMode method:
- Cachemode.normal: Normal mode (default), session reads and writes data to the level two cache.
- Cachemode.ignore: Ignore mode, session does not read to level two cache and does not write data.
- CacheMode.GET:Session the level two cache is read-only and does not write.
- CacheMode.PUT:Session write-only to the level two cache.
- Cachemode.refresh: Refresh mode, session does not read data from the level two cache, but it writes data read from the database. The difference between the and put is that refresh ignores the Hibernate.cache.use_minimal_puts property in the configuration file and forces all data in the level two cache to be refreshed.
??
??
NOTES: Hibernate Query cache