session flush方法主要做了两件事:
1. 清理缓存
2. 执行sql
session在什么情况下执行flush:
1. 默认在事务提交时
2. 显示的调用flush
3. 在执行查询前,如:iterate
注:hibernate按照save(insert),update、delete顺序提交相关操作,如:
Hibernate: insert into t_user3 (name, password, create_time, expire_time, user_id) values (?, ?, ?, ?, ?)
Hibernate: insert into t_user3 (name, password, create_time, expire_time, user_id) values (?, ?, ?, ?, ?)
Hibernate: update t_user3 set name=?, password=?, create_time=?, expire_time=? where user_id=?
我们可以利用flush来执行一个查询,再按hibernate的提交顺序进行,如:
Hibernate: insert into t_user3 (name, password, create_time, expire_time, user_id) values (?, ?, ?, ?, ?)
Hibernate: update t_user3 set name=?, password=?, create_time=?, expire_time=? where user_id=?
Hibernate: insert into t_user3 (name, password, create_time, expire_time, user_id) values (?, ?, ?, ?, ?)