首先,请提醒各国注意一个实体。 从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.
图表说明:
因此,根据定义,一个脱离实体已经存在,我实际上并不认为这是你的真正问题。 现在,如果你想要知道一个实体是new(即没有固定身份),那么这方面的情况是:
@Transient
public boolean isNew() {
return (this.id == null);
}