Explain in detail the use of cache and native SQL statements in the Hibernate framework of Java

Hibernate cache is all about application performance optimization and it is located in the application and @ R_ 419_ 2416 @ to avoid @ r_ 419_ 2416 @ multiple visits for better performance of performance critical applications.

Cache is very important to hibernate. It adopts a multi-level cache scheme, as described below:

First level cache: the first level cache is the session cache, which is a mandatory cache through which all requests must pass. The session object is an object that continuously its own power and is submitted to @ R_ 419_ 2416 @ before.

If multiple updates are issued to an object, hibernate tries to delay the update as long as possible to reduce the number of update SQL statements issued. If you close the session, all cached objects will be lost, either persistent or in @ R_ 419_ Updated in 2416 @.

L2 cache: L2 cache is an optional cache and L1 cache. It will always consult any L2 cache trying to find an object. The second level cache can be configured on the basis of each class and each collection, and is mainly responsible for the objects cached in the session.

Any third-party cache can use hibernate. org. hibernate. cache. Provided by the cacheprovider interface, a handle cache implementation of Hibernate must be implemented.

Query level cache: Hibernate also realizes the close integration of query result set cache and secondary cache.

This is an optional feature that requires two additional physical caches to store cached query results and regions when the timestamp of the last update of a table. This is useful only for queries that often run with the same parameters.

L2 cache: Hibernate uses L1 cache. By default, you do nothing and use L1 cache. Let's go straight to the optional second level cache. Not all classes benefit from caching, so it is important to disable L2 caching

Hibernate L2 cache is set to two steps. First, you must decide which concurrency policy to use. After that, you can configure cache expiration and use cache to provide physical cache properties.

Concurrency policy: concurrency policy is an intermediary responsible for storing data items in the cache and retrieving them from the cache. If you want to enable L2 caching, you will have to decide which cache concurrency policy to use for each persistent class and collection.

Transactional: using this strategy is the main place to read data to prevent concurrent transactions of outdated data, which is crucial in rare cases of update.

Read write: using this strategy again is the main place to read data to prevent concurrent transactions. Stale data is very important in rare cases of update.

Nonstrict read write: this policy does not guarantee the cache and @ R_ 419_ 2416 @ consistency between. With this strategy, if the data is rarely changed and the possibility of stale data is very small, the key is not to pay attention.

Read only: the concurrency policy applies to data and will never change. The data used are for reference only.

If we want to use the second level cache for our employee class, let's add the mapping elements required to tell hibernate to use the read-write cache policy for the employee instance.

The usage = "read-write" attribute tells hibernate to use a read-write concurrency policy to define the cache.

Cache provider: after considering the concurrency strategy that will use your cache candidate class, the next step is to select a cache provider. Hibernate forces the selection of a cache to provide the entire application.

Specify hibernate cfg. Cache provider in XML configuration file. Select ehcache as the second level cache provider:

Now, you need to specify the properties of the cache region. Ehcache has its own configuration file XML, in the application, in classpath. In ehcache The Employee class cache configuration in XML may look like this:

In this way, the L2 cache of the Employee class and Hibernate L2 cache are now enabled whenever an employee is browsed or when an employee is loaded through an identifier.

You should analyze all your classes and select the appropriate caching strategy for each class. Sometimes, L2 caching can degrade the performance of your application. Therefore, it is recommended that the benchmark application does not enable caching for the first time, which is very suitable for caching and checking performance. If the cache does not improve the system performance, it is meaningless to make any type of cache.

Query level cache: to use query cache, you must first use hibernate cache. use_ query_ The cache = "true" attribute activates it in the configuration file. If this property is set to true, let hibernate create the required cache in memory to save the query and identifier set.

Next, using the query cache, you can use the setcacheable (Boolean) method of the query class. For example:

Hibernate also supports very fine-grained caching through the concept of a cache area. The cache is part of a cache that is given a name.

This code uses the method to tell hibernate to store and find employee queries in the cache.

Hibernate native SQL can use native SQL to express @ R_ 419_ 2416 @ query, if you want to use @ r_ 419_ 2416 @ special functions, such as query prompt or connect keyword in Oracle. Hibernate3. X allows the use of handwritten SQL statements, including stored procedures, all creation, update, delete and load operations.

The application will create a native SQL query (on the session interface) from the session. Createsqlquery() method:

Public sqlQuery createsqlquery (string sqlstring) throws HibernateException when passing an SQL query to the createsqlquery() method, you can join the SQL result with any existing hibernate entity, or a scalar result using the addentity() method, addjoin() and addscalar() methods.

Scalar query: the most basic SQL query is to get a list of scalars (values) from one or more tables. The following syntax uses the values of native SQL scalars:

Entity query: all the above queries return scalar values, that is, the "raw" data returned from the resultset. The following is the syntax to obtain the entity object as a whole from the native SQL query through the addentity () method.

Named SQL query: the following syntax is used to obtain entity objects from native SQL queries and use named SQL queries through the addentity () method.

Native SQL example: consider the following POJO classes:

Let's create the following employee table to store the employee object:

The following files will be mapped.

Finally, we will create the main () method of the application class to run. We will use the application of native SQL query:

Compile and execute: here are the steps to compile and run the above application. Ensure that path and classpath are set appropriately before compiling and executing.

Execute the manageemployee binary to run the program.

The following results will be obtained and the record will be created in the employee table.

If you check the employee table, it should record that:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>