Home  >  Article  >  Java  >  Introduction to Hibernate's second-level cache and multi-table query in Java

Introduction to Hibernate's second-level cache and multi-table query in Java

不言
不言forward
2018-12-13 10:01:561401browse

This article brings you an introduction to Hibernate's second-level cache and multi-table query in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Let’s look at two interview questions about Hibernate in the Java framework. This is a common knowledge point about Hibernate.

1. Please introduce Hibernate's second-level cache.

Answer the question according to the following ideas:

(1) First, explain clearly what is Cache;

(2) Besides, the Session with hibernate is the first-level cache. That is, with the first-level cache, why do we need the second-level cache;

(3) Finally, let’s talk about how to configure it. Hibernate's second-level cache.

Cache is to store objects previously queried and used from the database in memory (in a data structure). This data structure is usually or similar to a Hashmap. When an object is to be used in the future, first Query whether the object exists in the cache. If so, use the object in the cache. If not, query the database and save the queried object in the cache for next time use. The following is the pseudo code of the cache:

leads to hibernate's second-level cache, and uses the following pseudo code to analyze the implementation principle of Cache.

Dao
{
hashmap map = new map();
User getUser(integer id)
{
User user = map.get(id)
if(user == null)
{
user = session.get(id);
map.put(id,user);
}
return user;
}
}
Dao
{
Cache cache = null
setCache(Cache cache)
{
this.cache = cache
}
User getUser(int id)
{
if(cache!=null)
{
User user = cache.get(id);
if(user ==null)
{
user = session.get(id);
cache.put(id,user);
}
return user;
}
return session.get(id);
}
}

Hibernate's Session is a kind of cache. We usually call it Hibernate's first-level cache. When you want to use session to query an object from the database, Session also first checks whether it exists internally. If the object exists, it will be returned directly. If it does not exist, it will access the database and save the query results internally. Since Session represents a session process, and a Session is associated with a database connection, it is best not to keep the Session open for a long time. It is usually only used in one transaction and should be closed at the end of the transaction. And Session is thread-unsafe and prone to problems when shared by multiple threads. Usually only the cache in the global sense is the real cache application and has greater cache value. Therefore, the cache function of Hibernate's Session level cache is not obvious and the application value is not great. Hibernate's second-level cache is to configure a global cache for Hibernate so that multiple threads and multiple transactions can share this cache. What we hope is that once one person has used it, others can also use it. Session does not have this effect.

The second-level cache is a software component independent of Hibernate and is a third-party product. Multiple vendors and organizations provide caching products, such as EHCache and OSCache, etc. To use the second-level cache in Hibernate, you must first configure which manufacturer's cache product to use in the hibernate.cfg.xml configuration file. Then you need to configure the cache product's own configuration file. Finally, you need to configure which entity objects in Hibernate should be included. to the management of the second level cache. After understanding the principle of second-level cache and having this idea, it is easy to configure Hibernate's second-level cache.

Extended knowledge: A SessionFactory can be associated with a second-level cache, that is, a second-level cache can only be responsible for caching data in one database. When using Hibernate's second-level cache, pay attention Do not have other applications or SessionFactory change the data in the current database, so the cached data will be inconsistent with the actual data in the database.

2. Hibernate performs multi-table queries and takes several fields from each table. That is to say, the query result set does not have a corresponding entity class. How to solve the problem?
Solution one, take out the data according to the Object[] data, and then form the bean yourself

Solution two, write a constructor for the bean of each table, for example, table one needs to find two fields1 and field2 field, then there is a constructor called Bean(type1 filed1, type2, field2), and then this bean can be generated directly in hql.

The above is the detailed content of Introduction to Hibernate's second-level cache and multi-table query in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete