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:
Any advice / other suggestions ?
I'm using JPA 1 with Hibernate 3.2 implementation and would prefer stick to the standard.
The solution is simple, just use the CascadeType. MERGE instead of CascadeType. PERSIST or CascadeType. ALL .
A PersistentEntity has a stable entity identifier, with which it can be accessed from the service implementation or other places. The state of an entity is persistent (durable) using Event Sourcing. We represent all state changes as events and those immutable facts are appended to an event log.
Detached instances may be made persistent by calling update() , saveOrUpdate() , lock() or replicate() . The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge() .
A detached entity (a.k.a. a detached object) is an object that has the same ID as an entity in the persistence store but that is no longer part of a persistence context (the scope of an EntityManager session).
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With