Home > Java > javaTutorial > Detailed explanation of caching and the use of native SQL statements in Java's Hibernate framework

Detailed explanation of caching and the use of native SQL statements in Java's Hibernate framework

高洛峰
Release: 2017-01-23 11:48:48
Original
1110 people have browsed it

Hibernate Cache
Cache is all about application performance optimization and it sits between the application and the database to avoid multiple database accesses and allow performance-critical applications to perform better.

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

Detailed explanation of caching and the use of native SQL statements in Javas Hibernate framework

First level cache:
First level cache It is the Session cache and is a mandatory cache through which all requests must pass. The Session object keeps its own power object, before being submitted to the database.

If multiple updates are issued for an object, Hibernate attempts to delay the updates for 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 persisted, or updated in the database.

Second level cache:
The second level cache is an optional cache and the first level cache will always be consulted before any attempt to find an object in the second level cache. The second level cache can be configured on a per-class and per-collection basis and is responsible for caching objects within the session.

Any third-party cache can use Hibernate. The org.hibernate.cache.CacheProvider interface provides a handle cache implementation that must be implemented to provide Hibernate.

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

This is an optional feature that requires two additional physical caches to hold the cached query results and the timestamp of the region when a table was last updated. This is only useful for queries that are run frequently using the same parameters.

Second level cache:
Hibernate uses the first level cache, by default you do nothing to use the first level cache. Let's jump right into the optional second level cache. Not all classes benefit from caching, so it is important to be able to disable the second-level cache

Hibernate second-level cache is set up as a two-step process. First, you must decide on the concurrency strategy to use. After this, you can configure cache expiration and use cache provisioning physical cache properties.

Concurrency strategy:
Concurrency strategy is an intermediary responsible for storing data items in the cache and retrieving them from the cache. If you want to enable second-level caching, you will have to decide, for each persistence class and collection, the cache concurrency strategy to use.

Transactional: It is critical to use this strategy where the main read data is to prevent concurrent transactions of stale data in the rare case of updates.

Read-write: Use this strategy again to primarily read data where it is critical to prevent concurrent transactions from stale data in the rare case of an update.

Nonstrict-read-write: This strategy does not guarantee consistency between the cache and the database. Using this strategy is not critical if the data rarely changes and the likelihood of stale data is small.

Read-only: Concurrency strategy applies to data and never changes. Usage data is for reference only.

If we are going to use second level caching for our Employee class, let's add the mapping elements required to tell Hibernate to use a read-write caching strategy for Employee instances.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
 
<hibernate-mapping>
  <class name="Employee" table="EMPLOYEE">
   <meta attribute="class-description">
     This class contains the employee detail. 
   </meta>
   <cache usage="read-write"/>
   <id name="id" type="int" column="id">
     <generator class="native"/>
   </id>
   <property name="firstName" column="first_name" type="string"/>
   <property name="lastName" column="last_name" type="string"/>
   <property name="salary" column="salary" type="int"/>
  </class>
</hibernate-mapping>
Copy after login

The usage="read-write" attribute tells Hibernate to use a cache defined by a read-write concurrency strategy.

Cache Provider:
After considering the concurrency strategy that will use your cache candidate class, the next step is to choose a cache provider. Hibernate forces the choice of one cache to serve the entire application.

Detailed explanation of caching and the use of native SQL statements in Javas Hibernate framework

The cache provided in the specified hibernate.cfg.xml configuration file. Select EHCache as the second-level cache provider:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
  <session-factory>
  <property name="hibernate.dialect">
   org.hibernate.dialect.MySQLDialect
  </property>
  <property name="hibernate.connection.driver_class">
   com.mysql.jdbc.Driver
  </property>
 
  <!-- Assume students is the database name -->
  <property name="hibernate.connection.url">
   jdbc:mysql://localhost/test
  </property>
  <property name="hibernate.connection.username">
   root
  </property>
  <property name="hibernate.connection.password">
   root123
  </property>
  <property name="hibernate.cache.provider_class">
   org.hibernate.cache.EhCacheProvider
  </property>
 
  <!-- List of XML mapping files -->
  <mapping resource="Employee.hbm.xml"/>
 
</session-factory>
</hibernate-configuration>
Copy after login

Now, you need to specify the properties of the cache area. EHCache has its own configuration file ehcache.xml, which is in the CLASSPATH of the application. The Employee class cache configuration in ehcache.xml might look like this:

<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
 
<cache name="Employee"
maxElementsInMemory="500"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
Copy after login

That's it, now enable the second level cache of the Employee class and Hibernate now has the second level cache whenever an employee is browsed or when passed Identifier loading employee.

You should analyze all your classes and choose the appropriate caching strategy for each class. Sometimes, the second level cache may degrade the performance of the application. So it is recommended to benchmark the application for the first time without caching enabled, perfect for caching and checking performance. If caching does not improve system performance then there is no point in having any kind of caching.

Query level cache:
To use the query cache, you must first activate it using the hibernate.cache.use_query_cache="true" attribute in the configuration file. If you set this property to true, let Hibernate create the required cache in memory to hold queries and identifier sets.

Next, to use the query cache, you can use the setCacheable(Boolean) method of the Query class. For example:

Session session = SessionFactory.openSession();
Query query = session.createQuery("FROM EMPLOYEE");
query.setCacheable(true);
List users = query.list();
SessionFactory.closeSession();
Copy after login

Hibernate也支持通过一个缓存区域的概念非常细粒度的缓存支持。缓存区是这是给定一个名称缓存的一部分。

Session session = SessionFactory.openSession();
Query query = session.createQuery("FROM EMPLOYEE");
query.setCacheable(true);
query.setCacheRegion("employee");
List users = query.list();
SessionFactory.closeSession();
Copy after login

此代码使用方法告诉Hibernate来存储和查找在缓存中的员工方面的查询。


Hibernate原生SQL
可以使用原生SQL来表达数据库查询,如果想利用数据库特有的功能,如查询提示或者Oracle中的CONNECT关键字。 Hibernate3.x允许使用手写SQL语句,包括存储过程,所有的创建,更新,删除和load操作。

应用程序将从会话创建一个原生SQL查询(Session接口上)createSQLQuery()方法:

public SQLQuery createSQLQuery(String sqlString) throws HibernateException
当传递一个包含SQL查询到createSQLQuery()方法,可以将SQL结果与任何现有的Hibernate实体,联接,或者一个标量结果使用addEntity()方法,addJoin(),和addScalar()方法关联的字符串。

标量查询:
最基本的SQL查询是从一个或多个表中得到标量(数值)的列表。以下是语法使用原生SQL标量的值:

String sql = "SELECT first_name, salary FROM EMPLOYEE";
SQLQuery query = session.createSQLQuery(sql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List results = query.list();
Copy after login

实体的查询:
上面的查询都是返回标量值,也就是从resultset中返回的“裸”数据。以下是语法通过addEntity()方法来从原生SQL查询获得实体对象作为一个整体。

String sql = "SELECT * FROM EMPLOYEE";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Employee.class);
List results = query.list();
Copy after login

命名SQL查询:
以下是语法通过addEntity()方法来从原生SQL查询获得实体对象和使用命名SQL查询。

String sql = "SELECT * FROM EMPLOYEE WHERE id = :employee_id";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Employee.class);
query.setParameter("employee_id", 10);
List results = query.list();
Copy after login

Native SQL 例子:
考虑下面的POJO类:

public class Employee {
  private int id;
  private String firstName;
  private String lastName; 
  private int salary;
 
  public Employee() {}
  public Employee(String fname, String lname, int salary) {
   this.firstName = fname;
   this.lastName = lname;
   this.salary = salary;
  }
  public int getId() {
   return id;
  }
  public void setId( int id ) {
   this.id = id;
  }
  public String getFirstName() {
   return firstName;
  }
  public void setFirstName( String first_name ) {
   this.firstName = first_name;
  }
  public String getLastName() {
   return lastName;
  }
  public void setLastName( String last_name ) {
   this.lastName = last_name;
  }
  public int getSalary() {
   return salary;
  }
  public void setSalary( int salary ) {
   this.salary = salary;
  }
}
Copy after login

让我们创建下面的EMPLOYEE表来存储Employee对象:

create table EMPLOYEE (
  id INT NOT NULL auto_increment,
  first_name VARCHAR(20) default NULL,
  last_name VARCHAR(20) default NULL,
  salary   INT default NULL,
  PRIMARY KEY (id)
);
Copy after login

以下将被映射文件。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping>
  <class name="Employee" table="EMPLOYEE">
   <meta attribute="class-description">
     This class contains the employee detail.
   </meta>
   <id name="id" type="int" column="id">
     <generator class="native"/>
   </id>
   <property name="firstName" column="first_name" type="string"/>
   <property name="lastName" column="last_name" type="string"/>
   <property name="salary" column="salary" type="int"/>
  </class>
</hibernate-mapping>
Copy after login

最后,我们将创建应用程序类的main()方法来运行,我们将使用原生SQL查询的应用程序:

import java.util.*;
  
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.SQLQuery;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.cfg.Configuration;
 
public class ManageEmployee {
  private static SessionFactory factory;
  public static void main(String[] args) {
   try{
     factory = new Configuration().configure().buildSessionFactory();
   }catch (Throwable ex) {
     System.err.println("Failed to create sessionFactory object." + ex);
     throw new ExceptionInInitializerError(ex);
   }
   ManageEmployee ME = new ManageEmployee();
 
   /* Add few employee records in database */
   Integer empID1 = ME.addEmployee("Zara", "Ali", 2000);
   Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);
   Integer empID3 = ME.addEmployee("John", "Paul", 5000);
   Integer empID4 = ME.addEmployee("Mohd", "Yasee", 3000);
 
   /* List down employees and their salary using Scalar Query */
   ME.listEmployeesScalar();
 
   /* List down complete employees information using Entity Query */
   ME.listEmployeesEntity();
  }
  /* Method to CREATE an employee in the database */
  public Integer addEmployee(String fname, String lname, int salary){
   Session session = factory.openSession();
   Transaction tx = null;
   Integer employeeID = null;
   try{
     tx = session.beginTransaction();
     Employee employee = new Employee(fname, lname, salary);
     employeeID = (Integer) session.save(employee);
     tx.commit();
   }catch (HibernateException e) {
     if (tx!=null) tx.rollback();
     e.printStackTrace();
   }finally {
     session.close();
   }
   return employeeID;
  }
 
  /* Method to READ all the employees using Scalar Query */
  public void listEmployeesScalar( ){
   Session session = factory.openSession();
   Transaction tx = null;
   try{
     tx = session.beginTransaction();
     String sql = "SELECT first_name, salary FROM EMPLOYEE";
     SQLQuery query = session.createSQLQuery(sql);
     query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
     List data = query.list();
 
     for(Object object : data)
     {
      Map row = (Map)object;
      System.out.print("First Name: " + row.get("first_name"));
      System.out.println(", Salary: " + row.get("salary"));
     }
     tx.commit();
   }catch (HibernateException e) {
     if (tx!=null) tx.rollback();
     e.printStackTrace();
   }finally {
     session.close();
   }
  }
 
  /* Method to READ all the employees using Entity Query */
  public void listEmployeesEntity( ){
   Session session = factory.openSession();
   Transaction tx = null;
   try{
     tx = session.beginTransaction();
     String sql = "SELECT * FROM EMPLOYEE";
     SQLQuery query = session.createSQLQuery(sql);
     query.addEntity(Employee.class);
     List employees = query.list();
 
     for (Iterator iterator =
              employees.iterator(); iterator.hasNext();){
      Employee employee = (Employee) iterator.next();
      System.out.print("First Name: " + employee.getFirstName());
      System.out.print(" Last Name: " + employee.getLastName());
      System.out.println(" Salary: " + employee.getSalary());
     }
     tx.commit();
   }catch (HibernateException e) {
     if (tx!=null) tx.rollback();
     e.printStackTrace();
   }finally {
     session.close();
   }
  }
}
Copy after login

编译和执行:
下面是步骤来编译并运行上述应用程序。请确保在进行的编译和执行之前,适当地设置PATH和CLASSPATH。

执行ManageEmployee二进制文件来运行程序。

会得到以下结果,并记录将在EMPLOYEE表中创建。

$java ManageEmployee
Copy after login
.......VARIOUS LOG MESSAGES WILL DISPLAY HERE........
 
First Name: Zara, Salary: 2000
First Name: Daisy, Salary: 5000
First Name: John, Salary: 5000
First Name: Mohd, Salary: 3000
First Name: Zara Last Name: Ali Salary: 2000
First Name: Daisy Last Name: Das Salary: 5000
First Name: John Last Name: Paul Salary: 5000
First Name: Mohd Last Name: Yasee Salary: 3000
Copy after login

如果检查EMPLOYEE表,它应该记录下已:

mysql> select * from EMPLOYEE;
Copy after login
+----+------------+-----------+--------+
| id | first_name | last_name | salary |
+----+------------+-----------+--------+
| 26 | Zara    | Ali    |  2000 |
| 27 | Daisy   | Das    |  5000 |
| 28 | John    | Paul   |  5000 |
| 29 | Mohd    | Yasee   |  3000 |
+----+------------+-----------+--------+
4 rows in set (0.00 sec)
Copy after login

更多Detailed explanation of caching and the use of native SQL statements in Javas Hibernate framework相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template