gpt4 book ai didi

当我手动更新行时,Java 实体管理器不会加载更改

转载 作者:行者123 更新时间:2023-12-01 21:15:37 24 4
gpt4 key购买 nike

我有一个由 guice 注入(inject)的简单服务。它使用 Hibernate、EntityManager。

class BrillantPaulaServiceImpl implements BrillantPaulaService {
@Inject
EntityManager em;

@Override
public Status EnqueueStatusCheck(Integer statusId) {
Status status = em.find(Status.class, statusId);
EntityTransaction transaction = em.getTransaction();
try {
//..... do some work
} finally {
if (transaction.isActive()) {
transaction.rollback();
}
}
}
return status;
}

当我从 pgsql 手动更新行时,“Status status = em.find(Status.class, statusId);”不查看更改。它返回旧实体。可能的问题是什么?

最佳答案

em.find(...) 的作用是,它首先检查持久性上下文,因为存在缓存的实体,所以它返回它而不是从数据库获取它。这是来自 here 的一些引用:

Find by primary key, using the specified properties. Search for an entity of the specified class and primary key. If the entity instance is contained in the persistence context, it is returned from there. If a vendor-specific property or hint is not recognized, it is silently ignored.

在使用缓存的情况下,JPA将从那里获取实体。仅当这些实体通过 JPA 修改时,它才会跟踪这些实体的更改。如果您自己更新底层数据,无论是直接更新还是通过其他外部系统更新,JPA 将不会意识到这些更改。

关于当我手动更新行时,Java 实体管理器不会加载更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40235884/

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