首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果我从我的代码中排除session.evict()会发生什么?

如果我从我的代码中排除session.evict()会发生什么?
EN

Stack Overflow用户
提问于 2020-04-30 14:57:05
回答 1查看 87关注 0票数 0

我对hibernate还不熟悉。我想知道我们是否有session.evict()的替代品。我已经注释了这一行,并且我的逻辑运行得很好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-30 15:05:33

session.evict()方法用于从缓存中删除与会话关联的特定对象。因此,删除它将强制hibernate从数据库中获取对象,而不是查看缓存。示例

代码语言:javascript
复制
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
try
{
SomeEntity myEntity= (SomeEntity) session.load(SomeEntity.class, new Integer(1)); //look in cache (will not be found) if not found then fetch from database
System.out.println(myEntity.getName());

myEntity = (SomeEntity) session.load(SomeEntity.class, new Integer(1)); //look in cache(will be found) if not then fetch from database
System.out.println(myEntity.getName());

session.evict(myEntity); // will remove from cache

myEntity = (SomeEntity) session.load(SomeEntity.class, new Integer(1)); // object will again be fetched from database if not found in second level cache
System.out.println(myEntity.getName());
}
finally
{
session.getTransaction().commit();
HibernateUtil.shutdown();
}

Edit : Session.evict()将从一级缓存中删除实体,从会话中删除对象后,对对象所做的任何更改都不会持久。如果使用cascade="evict“映射关联,则关联的对象也将被分离。

希望它能有所帮助!!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61517567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档