0

How you can know if the persist method worked or not? I only can think in two solutions for this, but for sure there are some more direct or prepared for this. Anyway, I want to know if somebody knows problems with this solutions or ar better alternative?. Thanks?

When I use persist I don't have a return to check if the method finish successfull or not. (Maybe boolean or the entity itself to check the id)

UserEntity e = new UserEntity();
e.setName("Name");
e.setAge(30);
em.persist(e);

Option#1 EntityManger contains method http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#contains

UserEntity e = new UserEntity();
e.setName("Name");
e.setAge(30);
em.persist(e);
----
return em.contains(e);

Option#2 Return the entity ID When I persist the entity, I can make commit and return the entity or the Entity id, if I have an id, the persist method should be worked success.

UserEntity e = new UserEntity();
e.setName("Name");
e.setAge(30);
em.persist(e);
em.commit();
----
return e;//or return e.getId();
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
Genaut
  • 1,810
  • 2
  • 29
  • 60

2 Answers2

1

Please refer to the JPA 2.1 specification for the details.

Option #1

About the contains() method:

3.2.8 Managed Instances

The contains() method can be used to determine whether an entity instance is managed in the current persistence context. The contains method returns true:

  • If the entity has been retrieved from the database or has been returned by getReference, and has not been removed or detached.
  • If the entity instance is new, and the persist method has been called on the entity or the persist operation has been cascaded to it.

JSR 338: Java™ Persistence 2.1, Final Release, page 86.

About the persist() method:

3.2.2 Persisting an Entity Instance

A new entity instance becomes both managed and persistent by invoking the persist method on it or by cascading the persist operation.

JSR 338: Java™ Persistence 2.1, Final Release, page 81.

There is a direct answer in the quotes.

Option #2

3.2.2 Persisting an Entity Instance

  • If X is a detached object, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.

JSR 338: Java™ Persistence 2.1, Final Release, page 81.

Therefore, consider using one of the following «manual» (i.e. explicit) method calls:

  • The entityManager.flush(); method call.
  • The entityManager.getTransaction().commit(); method call.

Also, please refer to the question: JPA - Returning an auto generated id after persist().

1

For the option 1 : em.contains(e) only checks whether the entity is in the managed state of the persistence context and nothing about the result so this is only to check whether the entity is in managed state or not

For option 2 : It will return the persisted value or straight away throw the exception if the persistence failed.

But once the entity is in managed state and it is persisted the entity will be updated with the data from the database so just returning the entity will suffice.

If you really want to flush the data instead of jpa doing itself call em.flush() this will flush the data and the the entity object will hold the persisted value from the database.

em.persist(e);
em.flush();   //not really required 
return e; //this will return the object that is already persisted in the database

check entity life cycle for more info

Harish Barma
  • 624
  • 8
  • 15