首页 > 代码库 > day37 03-Hibernate二级缓存:集合缓冲区特点

day37 03-Hibernate二级缓存:集合缓冲区特点

技术分享

所以说要经常检查hibernate3的核心配置文件hibernate.cfg.xml.


 

技术分享

Hibernate:
select
customer0_.cid as cid0_0_,
customer0_.version as version0_0_,
customer0_.cname as cname0_0_,
customer0_.age as age0_0_
from
customer customer0_
where
customer0_.cid=?
Hibernate:
select
orders0_.cno as cno0_1_,
orders0_.oid as oid1_,
orders0_.oid as oid1_0_,
orders0_.addr as addr1_0_,
orders0_.cno as cno1_0_
from
orders orders0_
where
orders0_.cno=?

订单的数量:11

技术分享技术分享

这是咱们的默认情况,fetch="select" lazy="tue"的情况。这种情况发多条SQL语句。所以不是连接查询,而是分步单表查询。

向下执行,查询customer2没有发SQL,因为使用了二级缓存。再向下执行查询customer2的订单也没有发SQL,跟hibernate3核心配置文件hibernate.cfg.xml中的一句话:<collection-cache usage="read-write" collection="cn.itcast.hibernate3.demo1.Customer.orders"/>有关。集合缓冲区 所以在获得客户的订单的数量的时候,就没有再去发送SQL语句了。因为它已经把我们的集合orders也给缓存了。集合缓存区用来缓存对象中的集合。我们现在用的就是对象中的集合,所以它没有发送SQL语句。

现在要证明集合缓存区的数据是依赖于类缓存区的。尝试注释掉<collection-cache usage="read-write" collection="cn.itcast.hibernate3.demo1.Customer.orders"/>这句话。

Hibernate:
select
customer0_.cid as cid0_0_,
customer0_.version as version0_0_,
customer0_.cname as cname0_0_,
customer0_.age as age0_0_
from
customer customer0_
where
customer0_.cid=?
Hibernate:
select
orders0_.cno as cno0_1_,
orders0_.oid as oid1_,
orders0_.oid as oid1_0_,
orders0_.addr as addr1_0_,
orders0_.cno as cno1_0_
from
orders orders0_
where
orders0_.cno=?
订单的数量:11
Hibernate:
select
orders0_.cno as cno0_1_,
orders0_.oid as oid1_,
orders0_.oid as oid1_0_,
orders0_.addr as addr1_0_,
orders0_.cno as cno1_0_
from
orders orders0_
where
orders0_.cno=?
订单的数量:11

取消了集合缓存区的话查询customer2不会发SQL(因为customer2使用了二级缓存)。而查询customer2的订单就会发SQL(因为取消了集合缓存区)。

day37 03-Hibernate二级缓存:集合缓冲区特点