gpt4 book ai didi

java - EntityManager.contains() 在 persist() 之后返回 false

转载 作者:搜寻专家 更新时间:2023-10-31 20:22:46 25 4
gpt4 key购买 nike

EntityManager.persist() 的文档说它将使一个实例被管理和持久化

它将实体保存到数据库中,但是当我调用 EntityManager.contains() 方法来检查我刚刚保存的实体是否被管理时,它返回 false.

我只想知道为什么会这样?也许有什么我没能做到或忽略的事情?

建议将不胜感激:D

最佳答案

这真的很奇怪。据Sun EJB3 spec :

The contains() method can be used to determine whether an entity instance is managed in the current persistence context.

The contains method returns true:

  • If the entity has been retrieved from the database, and has not been removed or detached.
  • If the entity instance is new, and the persist method has been called on the entity or the persist operation has been cascaded to it.

The contains method returns false:

  • If the instance is detached.
  • If the remove method has been called on the entity, or the remove operation has been cascaded to it.
  • If the instance is new, and the persist method has not been called on the entity or the persist operation has not been cascaded to it.

Note that the effect of the cascading of persist or remove is immediately visible to the contains method, whereas the actual insertion or deletion of the database representation for the entity may be deferred until the end of the transaction.

您是否在同一个事务中调用 contains

关于java - EntityManager.contains() 在 persist() 之后返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8529542/

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