Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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):</p> <blockquote> <p>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.</p> <ul> <li>A <strong>new</strong> entity instance has <strong>no persistent identity</strong>, and is not yet associated with a persistence context.</li> <li>A <strong>managed</strong> entity instance is an instance <strong>with a persistent identity</strong> that is currently associated with a persistence context.</li> <li>A <strong>detached</strong> entity instance is an instance <strong>with a persistent identity</strong> that is not (or no longer) associated with a persistence context.</li> <li>A <strong>removed</strong> entity instance is an instance with a persistent identity, associated with a persistence context, that is scheduled for removal from the database.</li> </ul> </blockquote> <p>And a graphical illustration:</p> <p><img src="https://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/img/jpa-state-transitions.png" alt="alt text"></p> <p>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 <em>new</em> (i.e. doesn't have any persistent identity), what about this:</p> <pre><code>@Transient public boolean isNew() { return (this.id == null); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload