English 中文(简体)
如何知道是否已经维持一个脱离后的日本人民军实体?
原标题:How to know if a detached JPA entity has already been persisted or not?

我在我申请的网上信标层中有一个日本邮局实体。 如果该实体在数据库中已经存在,或只在用户会议上存在,我随时想知道。

这将是在业务层面,我将使用实体经理(实体)方法,但我认为,在我的核心层次上,我需要一个额外特性,表明该实体是否得到拯救。 如何执行? 现在考虑以下选择:

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

任何建议或其他建议?

I m使用JPA 1,Hibernate 3.2 执行,倾向于遵守标准。

最佳回答

首先,请提醒各国注意一个实体。 从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 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.

图表说明:

“alt

因此,根据定义,一个脱离实体已经存在,我实际上并不认为这是你的真正问题。 现在,如果你想要知道一个实体是new(即没有固定身份),那么这方面的情况是:

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

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签