脏检查不适用于来自另一个线程的附加实体

Dirty check isn't working with attached entity from another thread

我有以下代码:

completableFuture
    .thenApply(x -> transactionTemplate.execute(s -> {
        repository.save(entity);
        entity.update();
        return entity;
    }));

entity 是来自另一个线程的实体,我必须在异步调用后处理它。问题是当我调用 entity.update() 并且更改了一些数据时,Hibernate 不会发出更新查询。如果我把 repository.save(entity) 放在 entity.update() 之后,一切正常,但对我来说似乎有点奇怪。

在这些情况下不应该进行脏检查吗?

如果我理解正确,您是在尝试通过调用 save 将实体附加到当前 EntityManager。为此,您应该使用从 save 返回的实体进行所有进一步的工作。

来自documentation

Saves a given entity. Use the returned instance for further operations as the save operation might have changed the entity instance completely.