我对hibernate还不熟悉。我想知道我们是否有session.evict()的替代品。我已经注释了这一行,并且我的逻辑运行得很好。
发布于 2020-04-30 15:05:33
session.evict()方法用于从缓存中删除与会话关联的特定对象。因此,删除它将强制hibernate从数据库中获取对象,而不是查看缓存。示例
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“映射关联,则关联的对象也将被分离。
希望它能有所帮助!!
https://stackoverflow.com/questions/61517567
复制相似问题