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

Posted by snowflake on Stack Overflow See other posts from Stack Overflow or by snowflake
Published on 2010-05-06T09:11:18Z Indexed on 2010/05/06 9:48 UTC
Read the original article Hit count: 225

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about jpa

Related posts about hibernate