hibernate中session的三种状态:
瞬时状态:没有被session所管理且数据库中没有。
持久化状态:被session所管理但是数据库中没有。
离线状态:数据库中存在但是未被session所管理。
下面是几个小例子:
瞬时状态-->持久化状态01输出:
Hibernate: insert into t_user (username, password, nikename, born) values (?, ?, ?, ?)
瞬时状态-->持久化状态02输出:
Hibernate: insert into t_user (username, password, nikename, born) values (?, ?, ?, ?)
Hibernate: update t_user set username=?, password=?, nikename=?, born=? where id=?
update的执行时机输出:
Hibernate: insert into t_user (username, password, nikename, born) values (?, ?, ?, ?)
Hibernate: update t_user set username=?, password=?, nikename=?, born=? where id=?
save或update的执行与否输出:
Hibernate: insert into t_user (username, password, nikename, born) values (?, ?, ?, ?)
load也会使对象变为持久化状态输出:
Hibernate: select user0_.id as id0_0_, user0_.username as username0_0_, user0_.password as password0_0_, user0_.nikename as nikename0_0_, user0_.born as born0_0_ from t_user user0_ where user0_.id=?
Hibernate: update t_user set username=?, password=?, nikename=?, born=? where id=?
两次事务共享session输出:
Hibernate: select user0_.id as id0_0_, user0_.username as username0_0_, user0_.password as password0_0_, user0_.nikename as nikename0_0_, user0_.born as born0_0_ from t_user user0_ where user0_.id=?
Hibernate: update t_user set username=?, password=?, nikename=?, born=? where id=?
save操作输出:
Hibernate: insert into t_user (username, password, nikename, born) values (?, ?, ?, ?)
在提交update的时候 怎么发出的sql语句???个人理解:如果该id对应的对象在数据库中不存在,则会抛出异常,如果数据库中存在,就会update,至于update的和session里的数据比较是不手动update才会触发的,如果直接执行update就不会再去session里比较了。
输出:
Hibernate: update t_user set username=?, password=?, nikename=?, born=? where id=?
输出:
Hibernate: update t_user set username=?, password=?, nikename=?, born=? where id=?
输出:
Hibernate: select user0_.id as id0_0_, user0_.username as username0_0_, user0_.password as password0_0_, user0_.nikename as nikename0_0_, user0_.born as born0_0_ from t_user user0_ where user0_.id=?
输出:
Hibernate: select user0_.id as id0_0_, user0_.username as username0_0_, user0_.password as password0_0_, user0_.nikename as nikename0_0_, user0_.born as born0_0_ from t_user user0_ where user0_.id=?
Hibernate: update t_user set username=?, password=?, nikename=?, born=? where id=?
个人理解:对比以上两个输出可以发现,手动update的时候不会和session中缓存进行比较,直接update,当不是手动update的时候,回和session缓存进行比较
网友评论