日期:2014-05-20  浏览次数:20594 次

hibernate中session.delete()的问题
这几天准备找工作,复习了一下hibernate,但是在复习缓存时遇到了如下问题,哪位高手能帮我解答一下?

第一个例子:
User user=(User)session.get(User.class, "3");//将id为3的user对象加入到session缓存中
session.delete(user);//这句代码对session结构来说执行了哪些操作?
user=(User)session.get(User.class, "3");//这里为什么没有发出sql语句,是因为session缓存中还有id为3的user对象吗?

第二个例子:
User user=(User)session.get(User.class, "3");//将id为3的user对象加入到session缓存中
session.delete(user);
session.flush();////这句代码对session结构来说执行了哪些操作?
user=(User)session.get(User.class, "3");//这里为什么发出了sql语句?

------解决方案--------------------
不知道你的事务是怎么处理的。。。按理说没有手动配置过2级缓存,2个例子都应该发出SQL语句的
你确定2个配置都一样吗?
那个flush的操作好像是属于缓存的管理,强制将数据持久化。
------解决方案--------------------
学习。。。。。。。。。。。。。。。。。
------解决方案--------------------
我个人的理解是:第一个例子中只是user对象在缓存中的不同状态,因为此时事务尚未提交。而第二个例子中flush的时候,事务已经进行了提交,此时已经把user从缓存中清除了,当你再次get的时候,缓存中已经不存在user对象了,所以它会再次发出sql去数据库中查找..
------解决方案--------------------
你把show_sql 设为true, 以及 cache的log设为debug。 后台日志都看的到了。
------解决方案--------------------
cache的log设为debug
------解决方案--------------------
Java code
[color=#333399]delete(Object object) [/color]          Remove a persistent instance from the datastore.

----

[color=#333399]get(Class clazz, Serializable id)[/color] 
          Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.

------解决方案--------------------
delete(Object object) Remove a persistent instance from the datastore.

----

get(Class clazz, Serializable id) 
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
------解决方案--------------------
围观下
------解决方案--------------------
cache的log设为debug,这个好像听说过,不过没实际用过

------解决方案--------------------
show_sql在hibernate.cfg.xml配置文件中,看一下hibernate的参考文档,其中就有show_sql会把对数据库的操作代码在控制台中显示出来。