Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a detached JPA entity has already been persisted or not?

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.

like image 843
snowflake Avatar asked May 06 '10 09:05

snowflake


People also ask

How do you resolve a detached entity passed to persist?

The solution is simple, just use the CascadeType. MERGE instead of CascadeType. PERSIST or CascadeType. ALL .

What is persisted entity?

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.

How do you persist a detached object in hibernate?

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() .

What is a detached entity in JPA?

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


1 Answers

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:

alt text

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); } 
like image 80
Pascal Thivent Avatar answered Sep 16 '22 20:09

Pascal Thivent