一. Hibernate的对象状态
1. 在Hibernate中,对象一共有三种状态,游离状态(有id没有session),瞬时状态(没有id没有session),持久化状态(也就是放到缓存中的对象,有id有session),所有保存到数据库的操作都是由持久化 状态下的对象去实现的。
//三种状态的特点
//save方法:其实不能理解为保存,理解成将瞬时状态转换成持久化状态的方法
//主键自增:执行save方法时,为了将对象转换为持久化状态,必须生成id值,所以需要执行insert语句
//increment:执行save方法,为了生成id,会执行查询id最大值的sql语句
//持久化状态的特点:持久化状态对象的任何变化都会自动同步到数据库中
public void demo2(){
//1. 获得Session
Session session = HibernateUtils.openSession();
//2. 控制事务
Transaction tx = session.beginTransaction();
//执行操作
Customer c = new Customer();//没有id,没有与session关联,瞬时状态
c.setCust_name("联想");//瞬时状态
session.save(c);//持久化状态,有id,有关联
//提交事务,关闭资源
tx.commit();
session.close();
}
//三种状态的特点
//持久化状态的特点:
public void demo3(){
//1. 获得Session
Session session = HibernateUtils.openSession();
//2. 控制事务
Transaction tx = session.beginTransaction();
//执行操作
Customer c = session.get(Customer.class, 1l);//持久化状态
c.setCust_name("微软公司");
session.save(c);//持久化状态,有id,有关联
//提交事务,关闭资源
tx.commit();
session.close();
}
二. Hibernate的缓存机制
1. 测试一级缓存,证明一级缓存存在
public void demo1(){
//1. 获得Session
Session session = HibernateUtils.openSession();
//2. 控制事务
Transaction tx = session.beginTransaction();
//执行操作
Customer c1 = session.get(Customer.class, 1l);
Customer c2 = session.get(Customer.class, 1l);
Customer c3 = session.get(Customer.class, 1l);
Customer c4 = session.get(Customer.class, 1l);
Customer c5 = session.get(Customer.class, 1l);
System.out.println(c3==c5);
//提交事务,关闭资源
tx.commit();
session.close();
}
Hibernate:
select
customer0_.cust_id as cust_id1_0_0_,
customer0_.cust_name as cust_nam2_0_0_,
customer0_.cust_source as cust_sou3_0_0_,
customer0_.cust_industry as cust_ind4_0_0_,
customer0_.cust_level as cust_lev5_0_0_,
customer0_.cust_phone as cust_pho6_0_0_,
customer0_.cust_mobile as cust_mob7_0_0_
from
cst_customer customer0_
where
customer0_.cust_id=?
true
//可以看到查询语句只执行了一次
说明缓存是为了提高查询效率
2. 缓存进阶-快照
public void demo2(){
//1. 获得Session
Session session = HibernateUtils.openSession();
//2. 控制事务
Transaction tx = session.beginTransaction();
//执行操作
Customer c1 = session.get(Customer.class, 1l);
c1.setCust_name("哈哈哈");
c1.setCust_name("传智播客");
//提交事务,关闭资源
tx.commit();
session.close();
}
//结果如下
Hibernate:
select
customer0_.cust_id as cust_id1_0_0_,
customer0_.cust_name as cust_nam2_0_0_,
customer0_.cust_source as cust_sou3_0_0_,
customer0_.cust_industry as cust_ind4_0_0_,
customer0_.cust_level as cust_lev5_0_0_,
customer0_.cust_phone as cust_pho6_0_0_,
customer0_.cust_mobile as cust_mob7_0_0_
from
cst_customer customer0_
where
customer0_.cust_id=?
Hibernate:
update
cst_customer
set
cust_name=?,
cust_source=?,
cust_industry=?,
cust_level=?,
cust_phone=?,
cust_mobile=?
where
cust_id=?
update语句只执行了一次,说明最开始的哈哈哈放到缓存和快照中,最后的传智播客只是修改了缓存对象,会在提交的时候去进行判断,缓存和快照是否一样,不一样就改变数据库。
快照是为了减少不必要的修改
3. 持久化状态对象其实就是放入session缓存中的对象
public void demo3(){
//1. 获得Session
Session session = HibernateUtils.openSession();
//2. 控制事务
Transaction tx = session.beginTransaction();
//执行操作
Customer c1 = new Customer();
c1.setCust_id(1l);//托管,游离状态
session.update(c1);
Customer c2 = session.get(Customer.class, 1l);
//提交事务,关闭资源
tx.commit();
session.close();
}
//结果如下
Hibernate:
update
cst_customer
set
cust_name=?,
cust_source=?,
cust_industry=?,
cust_level=?,
cust_phone=?,
cust_mobile=?
where
cust_id=?
可以看到只是 执行了第一个update的操作,第二个 get操作并没有打印sql语句,说明session缓存中已经有了。