对类对象进行缓存:
Customer.hbm.xml:
<class name="basicCar.bean.Customer" table="customer" lazy="true">
<cache usage="read-write"/>
***
<!-- 一对多,客户对账号-->
<set name="Accounts" inverse="true" cascade="delete">
<cache usage="read-write"/>
</class>
Account.hbm.xml:
<class name="basicCar.bean.Account" table="account" lazy="false" dynamic-update="true" dynamic-insert="true" optimistic-lock="version">
<cache usage="read-write"/>
void queryEntity2() //测试二级缓存和查询缓存
{
Session session1 = sf.openSession();
try{
Query query = session1.createQuery("from Account");
java.util.List list = query.list();
for(int i=0;i<list.size();i++)
{
Account ac1 = (Account)list.get(i);
System.out.println("id for the selectedaccout is:"
+ac1.getId_a());
}
session1.close();
//第二个session
Session session2 = sf.openSession();
Account ac2 = (Account)session2.load(Account.class, new Long(34));
System.out.println("money for the selectedaccount is:"+ac2.getMoney());
session2.close();
}catch (Exception e) {
System.out.println(e);
}
}
结果:
未配置<cache usage="read-write"/>前:
Hibernate: select account0_.id_a as id1_0_, account0_.version as version0_, account0_.money as money0_, account0_.opendate as opendate0_, account0_.customerId as customerId0_ from account account0_
id for the selectedaccout is:27
id for the selectedaccout is:28
id for the selectedaccout is:29
id for the selectedaccout is:30
id for the selectedaccout is:31
id for the selectedaccout is:32
id for the selectedaccout is:33
id for the selectedaccout is:34
id for the selectedaccout is:35
id for the selectedaccout is:36
id for the selectedaccout is:37
id for the selectedaccout is:38
id for the selectedaccout is:39
id for the selectedaccout is:40
id for the selectedaccout is:41
id for the selectedaccout is:42
id for the selectedaccout is:43
id for the selectedaccout is:44
id for the selectedaccout is:45
Hibernate: select account0_.id_a as id1_0_0_, account0_.version as version0_0_, account0_.money as money0_0_, account0_.opendate as opendate0_0_, account0_.customerId as customerId0_0_ from account account0_ where account0_.id_a=?
money for the selectedaccount is:555
end
配置后:
Hibernate: select account0_.id_a as id1_0_, account0_.version as version0_, account0_.money as money0_, account0_.opendate as opendate0_, account0_.customerId as customerId0_ from account account0_
id for the selectedaccout is:27
id for the selectedaccout is:28
id for the selectedaccout is:29
id for the selectedaccout is:30
id for the selectedaccout is:31
id for the selectedaccout is:32
id for the selectedaccout is:33
id for the selectedaccout is:34
id for the selectedaccout is:35
id for the selectedaccout is:36
id for the selectedaccout is:37
id for the selectedaccout is:38
id for the selectedaccout is:39
id for the selectedaccout is:40
id for the selectedaccout is:41
id for the selectedaccout is:42
id for the selectedaccout is:43
id for the selectedaccout is:44
id for the selectedaccout is:45
money for the selectedaccount is:555//少了一句查询
end
对集合进行缓存:
void saveEntity2() //对集合进行缓存
{
Session session = sf.openSession();
Transaction tx1 = session.beginTransaction();
//普通存储
Date time= new java.sql.Date(new java.util.Date().getTime());//获得系统时间.
String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);//将时间格式转换成符合Timestamp要求的格式.
Timestamp goodsC_date = Timestamp.valueOf(nowTime);//把时间转换
Customer ss = (Customer) session.load(Customer.class, new Long(299));
Account co1 = new Account(555,goodsC_date,1);
Account co2 = new Account(555,goodsC_date,1);
Set accounts = new HashSet();
accounts.add(co1);
accounts.add(co2);
ss.setAccounts(accounts);
co1.setCustomer(ss);
co2.setCustomer(ss);
//save先后顺序要先ss在co1
session.save(ss);
session.save(co1);
session.save(co2);
tx1.commit();
// close session
session.close();
}
由于Customer.hbm.xml配置了<cache>元素,载入两个accout时,会从缓存里读取,如果不配置将数据库读取。
查询缓存:
如ehcache.xml:
<ehcache>
<diskStore path="C:\\temp"/>
<!-- 使用二级缓存 -->
<defaultCache
maxElementsInMemory="5"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
overflowToDisk="true"
/>
<cache name="basicCar.bean.BasicCar"
maxElementsInMemory="5"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
overflowToDisk="true"
/>
<!-- 使用查询缓存,保存查询结果集 -->
<!-- 使用查询缓存,要保证二级缓存可用,所以二者的缓存有效时间设置为一样较好-->
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="50"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
overflowToDisk="true"
/>
<!-- 设置时间戳缓存的数据过期策略 ,保存每个表的最近更新时间-->
<!-- 查看它的生成时间是不是最后更新时间,如果不是,则证明这个查询缓存已经过期了。
因此只要更新过一个表,涉及这个表的查询缓存就过期了-->
<cache name="org.hibernate.cache.UpdateTimestampsCahe"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true"
/>
</ehcache>
queryEntity3():
void queryEntity3() //对查询进行缓存
{
Session session1 = sf.openSession();
try{
Query query = session1.createQuery("from Account");
//设置使用查询缓存
query.setCacheable(true);
query.setCacheRegion("basiCar.bean.Account");
java.util.List list = query.list();
for(int i=0;i<list.size();i++)
{
Account ac1 = (Account)list.get(i);
System.out.println("id for the selectedaccout is:"
+ac1.getId_a());
}
session1.close();
//第二个session
Session session2 = sf.openSession();
Query query2 = session2.createQuery("from Account");
//设置使用查询缓存
query2.setCacheable(true);
query2.setCacheRegion("basiCar.bean.Account");
java.util.List list2 = query2.list();
for(int i=0;i<list2.size();i++)
{
Account ac2 = (Account)list2.get(i);
System.out.println("id for the selectedaccout is:"
+ac2.getId_a());
}
session2.close();
}catch (Exception e) {
System.out.println(e);
}
}
Hibernate: select account0_.id_a as id1_0_, account0_.version as version0_, account0_.money as money0_, account0_.opendate as opendate0_, account0_.customerId as customerId0_ from account account0_
id for the selectedaccout is:27
id for the selectedaccout is:28
id for the selectedaccout is:29
id for the selectedaccout is:30
id for the selectedaccout is:31
id for the selectedaccout is:32
id for the selectedaccout is:33
id for the selectedaccout is:34
id for the selectedaccout is:35
id for the selectedaccout is:36
id for the selectedaccout is:37
id for the selectedaccout is:38
id for the selectedaccout is:39
id for the selectedaccout is:40
id for the selectedaccout is:41
id for the selectedaccout is:42
id for the selectedaccout is:43
id for the selectedaccout is:44
id for the selectedaccout is:45
id for the selectedaccout is:46
id for the selectedaccout is:47
id for the selectedaccout is:48
id for the selectedaccout is:49
id for the selectedaccout is:50
id for the selectedaccout is:51
id for the selectedaccout is:52
id for the selectedaccout is:53
id for the selectedaccout is:27
id for the selectedaccout is:28
id for the selectedaccout is:29
id for the selectedaccout is:30
id for the selectedaccout is:31
id for the selectedaccout is:32
id for the selectedaccout is:33
id for the selectedaccout is:34
id for the selectedaccout is:35
id for the selectedaccout is:36
id for the selectedaccout is:37
id for the selectedaccout is:38
id for the selectedaccout is:39
id for the selectedaccout is:40
id for the selectedaccout is:41
id for the selectedaccout is:42
id for the selectedaccout is:43
id for the selectedaccout is:44
id for the selectedaccout is:45
id for the selectedaccout is:46
id for the selectedaccout is:47
id for the selectedaccout is:48
id for the selectedaccout is:49
id for the selectedaccout is:50
id for the selectedaccout is:51
id for the selectedaccout is:52
id for the selectedaccout is:53
end
把query2.setcachable(false);即不使用查找缓存:
Hibernate: select account0_.id_a as id1_0_, account0_.version as version0_, account0_.money as money0_, account0_.opendate as opendate0_, account0_.customerId as customerId0_ from account account0_
id for the selectedaccout is:27
id for the selectedaccout is:28
id for the selectedaccout is:29
id for the selectedaccout is:30
id for the selectedaccout is:31
id for the selectedaccout is:32
id for the selectedaccout is:33
id for the selectedaccout is:34
id for the selectedaccout is:35
id for the selectedaccout is:36
id for the selectedaccout is:37
id for the selectedaccout is:38
id for the selectedaccout is:39
id for the selectedaccout is:40
id for the selectedaccout is:41
id for the selectedaccout is:42
id for the selectedaccout is:43
id for the selectedaccout is:44
id for the selectedaccout is:45
id for the selectedaccout is:46
id for the selectedaccout is:47
id for the selectedaccout is:48
id for the selectedaccout is:49
id for the selectedaccout is:50
id for the selectedaccout is:51
id for the selectedaccout is:52
id for the selectedaccout is:53
Hibernate: select account0_.id_a as id1_0_, account0_.version as version0_, account0_.money as money0_, account0_.opendate as opendate0_, account0_.customerId as customerId0_ from account account0_
id for the selectedaccout is:27
id for the selectedaccout is:28
id for the selectedaccout is:29
id for the selectedaccout is:30
id for the selectedaccout is:31
id for the selectedaccout is:32
id for the selectedaccout is:33
id for the selectedaccout is:34
id for the selectedaccout is:35
id for the selectedaccout is:36
id for the selectedaccout is:37
id for the selectedaccout is:38
id for the selectedaccout is:39
id for the selectedaccout is:40
id for the selectedaccout is:41
id for the selectedaccout is:42
id for the selectedaccout is:43
id for the selectedaccout is:44
id for the selectedaccout is:45
id for the selectedaccout is:46
id for the selectedaccout is:47
id for the selectedaccout is:48
id for the selectedaccout is:49
id for the selectedaccout is:50
id for the selectedaccout is:51
id for the selectedaccout is:52
id for the selectedaccout is:53
end
使用查找缓存的注意事项:
必须每次使用同一块缓存区域,条件也要相同,即query key要相同
Query query = session1.createQuery("from Account");
Query query2 = session2.createQuery("from Account where id>10");
上述就不会去查询缓存。