如何知道已分离的JPA实体是否已被保留? [英] How to know if a detached JPA entity has already been persisted or not?

查看:116
本文介绍了如何知道已分离的JPA实体是否已被保留?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的Web UI层中有一个JPA实体实例。我想随时知道这个实体是否已经存在于数据库中,或只存在于用户会话中。



它将在业务层,我会使用entitymanager.contains(Entity)方法,但在我的UI层中,我认为我需要一个额外的属性来指示实体是否已保存。如何实现?我正在考虑以下选项:


  • JPA属性的默认值由数据库设置,但会强制重新读取
  • 在我的代码中手动设置的非JPA属性或由JPA自动设置?



任何建议/其他建议?



我使用的是Hibernate 3.2实现的JPA 1,并且更喜欢坚持标准。

解决方案

首先,让我们提醒一个实体的各种状态。从JPA 1.0规范(在第3.2节实体实例的生命周期中):


本节描述用于管理$的
EntityManager操作b $ b实体实例的生命周期。
实体实例的特征可能是
,因为它们是新的,托管的,分离的或
被删除的。


  • 新的实体实例具有没有持久性标识,并且尚未与持久化上下文关联

  • A 托管实体实例是具有持久性标识的实例
    ,它当前与持久性上下文相关联。

  • strong> detached 实体实例是一个具有持久性标识的实例
    ,它不会(或不再)与持久性上下文相关联。

  • 已删除实体实例是一个具有持久性标识的实例,与持久性上下文关联,计划从数据库中删除。

图示:



因此,根据定义,脱离的实体已经被坚持,我其实并不认为这是你真正的问题。现在,如果你想知道一个实体是否是新的 (即没有任何持久的标识),那么这是什么:

  @Transient 
public boolean isNew(){
return(this.id == null);
}


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.

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:

  • 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?

Any advice / other suggestions ?

I'm using JPA 1 with Hibernate 3.2 implementation and would prefer stick to the standard.

解决方案

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):

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 new entity instance has no persistent identity, and is not yet associated with a persistence context.
  • A managed entity instance is an instance with a persistent identity that is currently associated with a persistence context.
  • A detached entity instance is an instance with a persistent identity that is not (or no longer) associated with a persistence context.
  • A removed entity instance is an instance with a persistent identity, associated with a persistence context, that is scheduled for removal from the database.

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);
}

这篇关于如何知道已分离的JPA实体是否已被保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆