Hibernate緩存
緩存是所有關于應用程序的性能優化和它位于應用程序和數據庫之間,以避免數據庫訪問多次,讓性能關鍵型應用程序有更好的表現。
緩存對Hibernate很重要,它采用了多級緩存方案下文所述:
第一級緩存:
第一級緩存是Session的緩存,是一個強制性的緩存,通過它所有的請求都必須通過。 Session對象不斷自身的動力的對象,提交到數據庫之前。
如果發出多個更新一個對象,Hibernate試圖拖延盡可能長的時間做了更新,以減少發出的更新SQL語句的數量。如果您關閉會話,所有被緩存的對象都將丟失,要么持久,或在數據庫中更新。
二級緩存:
二級緩存是可選的緩存和一級緩存,總是會征詢任何試圖找到一個對象的二級緩存之前。第二級緩存可以在每個類和每個集合基礎上進行配置,主要負責在會話緩存的對象。
任何第三方緩存可以使用Hibernate。org.hibernate.cache.CacheProvider接口提供,必須實施提供Hibernate一個句柄緩存實現。
查詢級別緩存:
Hibernate也實現了查詢結果集緩存與二級緩存的緊密集成在一起。
這是一個可選功能,需要兩個額外的物理緩存中保存緩存的查詢結果和地區當一個表的最后更新的時間戳。這只是針對那些使用相同的參數經常運行的查詢非常有用。
二級緩存:
Hibernate使用一級緩存,默認情況下,你什么都沒有做使用第一級緩存。讓我們直接進入可選的第二級緩存。并不是所有的類受益于緩存,這樣一來就能禁用二級緩存是很重要的
Hibernate二級緩存被設置為兩個步驟。首先,必須決定要使用的并發策略。在此之后,可以配置緩存過期和使用緩存提供物理緩存屬性。
并發策略:
并發策略是一個中介的負責存儲數據項在緩存并從緩存中檢索它們。如果要啟用二級緩存,將必須決定,為每個持久化類和集合,要使用的緩存并發策略。
Transactional: 使用這種策略的主要讀取數據的地方,以防止過時的數據的并發事務,在更新的罕見情況下是至關重要的。
Read-write: 再次使用這種策略的主要讀取數據的地方,以防止并發事務陳舊的數據是至關重要的,在更新的罕見情況。
Nonstrict-read-write: 這種策略不保證緩存與數據庫之間的一致性。使用此策略,如果數據很少改變和陳舊數據的可能性很小關鍵是不關注。
Read-only: 并發策略適用于數據,永遠不會改變。使用數據僅供參考。
如果我們要使用第二級緩存為我們的Employee類,讓我們添加告訴Hibernate使用可讀寫的高速緩存策略Employee實例所需的映射元素。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<? 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 > |
usage="read-write" 屬性告訴Hibernate使用一個可讀寫的并發策略定義的緩存。
緩存提供者:
考慮到會用你的緩存候選類的并發策略后,下一步就是選擇一個緩存提供程序。Hibernate迫使選擇一個緩存提供整個應用程序。
在指定hibernate.cfg.xml配置文件中的緩存提供。選擇EHCache作為第二級緩存提供程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<? 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 > |
現在,需要指定緩存區域的屬性。EHCache都有自己的配置文件ehcache.xml,在應用程序在CLASSPATH中。在ehcache.xml中Employee類高速緩存配置可能看起來像這樣:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< 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" /> |
就這樣,現在啟用Employee類的二級緩存和Hibernate現在二級緩存,每當瀏覽到一個雇員或當通過標識符加載雇員。
應該分析你所有的類,并選擇適當的緩存策略為每個類。有時,二級緩存可能降級的應用程序的性能。所以建議到基準應用程序第一次沒有啟用緩存,非常適合緩存和檢查性能。如果緩存不提高系統性能再有就是在使任何類型的緩存是沒有意義的。
查詢級別緩存:
使用查詢緩存,必須先使用 hibernate.cache.use_query_cache="true"屬性配置文件中激活它。如果將此屬性設置為true,讓Hibernate的在內存中創建所需的高速緩存來保存查詢和標識符集。
接下來,使用查詢緩存,可以使用Query類的setCacheable(Boolean)方法。例如:
1
2
3
4
5
|
Session session = SessionFactory.openSession(); Query query = session.createQuery( "FROM EMPLOYEE" ); query.setCacheable( true ); List users = query.list(); SessionFactory.closeSession(); |
Hibernate也支持通過一個緩存區域的概念非常細粒度的緩存支持。緩存區是這是給定一個名稱緩存的一部分。
1
2
3
4
5
6
|
Session session = SessionFactory.openSession(); Query query = session.createQuery( "FROM EMPLOYEE" ); query.setCacheable( true ); query.setCacheRegion( "employee" ); List users = query.list(); SessionFactory.closeSession(); |
此代碼使用方法告訴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標量的值:
1
2
3
4
|
String sql = "SELECT first_name, salary FROM EMPLOYEE" ; SQLQuery query = session.createSQLQuery(sql); query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); List results = query.list(); |
實體的查詢:
上面的查詢都是返回標量值,也就是從resultset中返回的“裸”數據。以下是語法通過addEntity()方法來從原生SQL查詢獲得實體對象作為一個整體。
1
2
3
4
|
String sql = "SELECT * FROM EMPLOYEE" ; SQLQuery query = session.createSQLQuery(sql); query.addEntity(Employee. class ); List results = query.list(); |
命名SQL查詢:
以下是語法通過addEntity()方法來從原生SQL查詢獲得實體對象和使用命名SQL查詢。
1
2
3
4
5
|
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(); |
Native SQL 例子:
考慮下面的POJO類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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; } } |
讓我們創建下面的EMPLOYEE表來存儲Employee對象:
1
2
3
4
5
6
7
|
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) ); |
以下將被映射文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<? 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 > |
最后,我們將創建應用程序類的main()方法來運行,我們將使用原生SQL查詢的應用程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
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(); } } } |
編譯和執行:
下面是步驟來編譯并運行上述應用程序。請確保在進行的編譯和執行之前,適當地設置PATH和CLASSPATH。
執行ManageEmployee二進制文件來運行程序。
會得到以下結果,并記錄將在EMPLOYEE表中創建。
1
|
$java ManageEmployee |
1
2
3
4
5
6
7
8
9
10
|
.......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 |
如果檢查EMPLOYEE表,它應該記錄下已:
1
|
mysql> select * from EMPLOYEE; |
1
2
3
4
5
6
7
8
9
|
+----+------------+-----------+--------+ | 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) |