java 如何知道一个分离的 JPA 实体是否已经被持久化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2779857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know if a detached JPA entity has already been persisted or not?
提问by snowflake
I have a JPA entity instance in the web UI layer of my application. I'd like to know at anytime if this entity has been already persisted in database or if it is only present in the user session.
我的应用程序的 Web UI 层中有一个 JPA 实体实例。我想随时知道这个实体是否已经保存在数据库中,或者它是否只存在于用户会话中。
It would be in the business layer, I would use entitymanager.contains(Entity) method, but in my UI layer I think I need an extra attribute indicating whether the entity has been saved or not. How implement that ? I'm considering following option for the moment:
它将在业务层中,我将使用 entitymanager.contains(Entity) 方法,但在我的 UI 层中,我认为我需要一个额外的属性来指示实体是否已保存。如何实施?我目前正在考虑以下选项:
- a JPA attribute with a default value set by the database, but would force a new read after each update ?
- a non JPA attribute manually set in my code or automatically set by JPA?
- 具有由数据库设置的默认值的 JPA 属性,但会在每次更新后强制进行新读取?
- 在我的代码中手动设置或由 JPA 自动设置的非 JPA 属性?
Any advice / other suggestions ?
任何建议/其他建议?
I'm using JPA 1 with Hibernate 3.2 implementation and would prefer stick to the standard.
我将 JPA 1 与 Hibernate 3.2 实现一起使用,并且更愿意坚持标准。
回答by Pascal Thivent
First, let's remind the various states of an entity. From the JPA 1.0 specification (in section 3.2 Entity Instance's Life Cycle):
首先,让我们提醒一个实体的各种状态。来自 JPA 1.0 规范(在第 3.2 节实体实例的生命周期中):
This section describes the EntityManager operations for managing an entity instance's lifecycle. An entity instance may be characterized as being new, managed, detached, or removed.
- A newentity instance has no persistent identity, and is not yet associated with a persistence context.
- A managedentity instance is an instance with a persistent identitythat is currently associated with a persistence context.
- A detachedentity instance is an instance with a persistent identitythat is not (or no longer) associated with a persistence context.
- A removedentity instance is an instance with a persistent identity, associated with a persistence context, that is scheduled for removal from the database.
本节描述了用于管理实体实例生命周期的 EntityManager 操作。实体实例可以表征为新的、管理的、分离的或移除的。
- 一个新的实体实例没有持久性标识,并且还没有与持久性上下文相关联。
- 受管实体实例是具有 当前与持久上下文相关联的持久标识的实例。
- 甲分离实体实例的实例与持久标识与一个持久上下文相关联不是(或不再)。
- 一个删除实体实例与持久标识,与一个持久化上下文相关联,即定于从数据库中取出一个实例。
And a graphical illustration:
和一个图形说明:


So, by definition, a detached entity has already been persisted, and I actually don't think that this is your real question. Now, if you want to know if an entity is new(i.e. doesn't have any persistent identity), what about this:
所以,根据定义,一个分离的实体已经被持久化了,我实际上并不认为这是你真正的问题。现在,如果你想知道一个实体是否是新的(即没有任何持久的身份),那么这个:
@Transient
public boolean isNew() {
return (this.id == null);
}

