QBC (Query by Criteria)
@Test public void getList(){ SessionFactory factory = HibernateUtils.getSessionFactory(); Session session = factory.getCurrentSession(); session.beginTransaction(); // Criteria criteria = session.createCriteria(Person.class); List<Person> list = criteria.list(); for(Person person : list){ System.out.println(person); } session.getTransaction().commit(); factory.close(); }
Paging Query
@Test public void getPage(){ SessionFactory factory = HibernateUtils.getSessionFactory(); Session session = factory.getCurrentSession(); session.beginTransaction(); // Criteria criteria = session.createCriteria(Person.class); criteria.setFirstResult(0); criteria.setMaxResults(2); List<Person> list = criteria.list(); for(Person person : list){ System.out.println(person); } session.getTransaction().commit(); factory.close(); }
Sort queries
Conditional query
Gt:greater Than, >
Ge:greater than or equivalent with, >=
Lt:less Than, <
Le:less than or equivalent with, <=
Eq:equal with, = =
Ne:not equal with,/=
Offline query
The first query statement of the Assembly, and in the past when the query to assemble a different, often is to optimize the settings.
@Test public void detachedCriteria(){ //假设此处是service层,在此组装查询条件 DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Person.class); detachedCriteria.add(Restrictions.like("name", "%p%")); detachedCriteria.add(Restrictions.gt("age", 32)); // dao SessionFactory factory = HibernateUtils.getSessionFactory(); Session session = factory.getCurrentSession(); session.beginTransaction(); //采用从业务层传递过来对象进行查询 Criteria criteria = detachedCriteria.getExecutableCriteria(session); List<Person> list = criteria.list(); for(Person person : list){ System.out.println(person); } session.getTransaction().commit(); factory.close(); }
Cache
Cache: A very general concept in the computer domain. It is between an application and a persistent data storage source, such as a file on a hard disk or a database, to reduce the frequency with which the application directly reads and writes to the hard disk (the permanent data storage source), thereby improving the operational performance of the application. The data in the cache is a copy of the data in the data storage source. The cached physical media is usually memory
Cache: Program <--(Memory)--hard drive
1.hibernate provides caching mechanisms: first-level cache, level two cache
- First-level caching: Session-level caching, which shares data in a single request.
- Second level cache: Sessionfactory level cache, the entire application shares a session factory, sharing a level two cache.
- The sessionfactory caches two parts:
A) built-in cache: use a map for storing configuration information, predefined SQL statements, etc., which are provided to the Hibernate framework for its own use and read-only. cannot be manipulated.
b) External cache: Use another map to hold user-defined data. Default does not open. The external cache hibernate only provides specifications (interfaces) and requires a third-party implementation class. The external cache has become a level two cache.
1. Data that fits into the level two cache:
- A) is rarely modified
- b) often accessed
- c) Not very important data, allowing occasional concurrency problems
2. Data that does not fit into the level two cache:
- A) are often modified
- b) Financial data, never allow concurrency problems
Cache Vendor:
1.EHCache: As a process (stand-alone) cache, the physical media that holds the data can be either memory or hard disk, which provides support for Hibernate's query caching. --Support cluster. ()
2.OpenSymphony ': Can be used as a process-wide cache, the physical media that holds the data can be memory or hard disk, provides a rich cache data expiration policy, provides support for Hibernate query caching
3.SwarmCache: Can be used as a cluster-wide cache, but does not support Hibernate's query cache
4.JBossCache: Available as a cluster-wide cache, Hibernate-enabled query caching ()
- Import jar Package: Ehcache-1.5.0.jar/commons-logging.jar/backport-util-concurrent.jar
- Turn on level two caching
- Determine level two cache provider
- Determining what needs to be cached
-
Configure the classes that require caching
-
Configure a collection that needs to be cached
- Configuring the Ehcache Custom configuration file
Non-strict read-write (Notstrict-read-write) does not guarantee database consistency between the cache and the database. When you use this policy, you should set sufficient cache expiration time, or you might read dirty data from the cache. This strategy can be used when some data is rarely changed and when the data and database have a small amount of impact.
interpret its configuration file
<defaultCache maxElementsInMemory="10000" // cache中最多保存对象的数量 eternal="false" // 缓存中的对象是否永久 timeToIdleSeconds="120" // 缓存数据的钝化时间(设置对象在过期之前的空闲时间) timeToLiveSeconds="120" // 缓存数据的生存时间(设置对象在过期之前的生存时间),如果用到再次加载 overflowToDisk="true" // 内存不足时,是否采用磁盘储存。 memoryStoreEvictionPolicy=" LRU " // 内存不足时对对象的清楚策略ehcache中缓存的三种情况策略FIFO:先进先出LFU:一直以来很少被使用LRU:最近很少被使用,清楚离储存数据的时间戳最远的对象
Query cache
1. Query cache, also known as Level three cache
2. The query cache is not used by default. Need to be turned on manually
3. Query cache: Binds the HQL statement to the query result. Content can be cached by hql the same statement.
A) By default, the query object will only store the results in the first level and level two cache, not from the first level or the level two cache.
b) Query caching allows query to get content from a level two cache.
Use steps
1. Turn on level two cache
2. In querying the Query object, set the contents of the cache (note: Both storage and query need to be set)
@Test /** * 查询集合 */ public void testQueryCache(){ SessionFactory factory = HibernateUtils.getSessionFactory(); Session session = factory.getCurrentSession(); session.beginTransaction(); // Query query = session.createQuery("from Person"); //设置使用查询缓存 query.setCacheable(true); query.list(); // session.getTransaction().commit(); //开启一个新的session Session session2 = factory.getCurrentSession(); session2.beginTransaction(); Query query2 = session2.createQuery("from Person"); query2.setCacheable(true); query2.list(); session2.getTransaction().commit(); factory.close(); }
Batch Processing
BULK INSERT
Batch Update
Bulk Delete
Consolidate C3P0 Connection pools Configure transaction Isolation level locks
- Optimistic lock
- Pessimistic lock
Integration Chapter
Learning Hibernate next Book