gpt4 book ai didi

java - 事务中hibernate删除实体后仍然可以选择

转载 作者:行者123 更新时间:2023-11-30 06:39:52 28 4
gpt4 key购买 nike

我有一种通过 hibernate 删除记录的方法。我删除后添加一个查询。并且删除的记录仍在结果列表中。不应该的。这是代码。

@Transactional
public void deleteFoo(long fooId){
Foo foo = fooDao.find(fooId);
fooDao.delete(foor);
List<Foo> brothers = fooDao.findByParentId(foo.getParentId());
// I think the brothers does not contains foo.
// Unfortunately, foo is still in the list.
// I do not know why and how to make sure the brothers does not contains foo.
...
}

该记录将从提交事务的数据库中删除。

最佳答案

您应该刷新 hibernate session 。

当您通过 Hibernate 处理数据库时,您正在使用 Hibernate session 。

Hibernate session 通过以下三种情况刷新到数据库。

  • commit()- 当您提交事务时
  • 运行查询之前
  • 当你调用session.flush()时

或者您可以通过以下方式手动冲洗

session.flush()

关于java - 事务中hibernate删除实体后仍然可以选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44560459/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com