0

Using Entity Manager, How to do we get know the row got inserted into database..

  public String insert(Object obj, String idGen){

        try{
            entityManager.getTransaction().begin();
            if (idGen == "auto") {
                entityManager.persist(obj); // here how to confirm the object was inserted or not into database
            } else if (idGen == "manual") {
                entityManager.persist(obj); // here how to confirm the object was inserted or not into database
            }
            entityManager.getTransaction().commit();
        }catch(Exception e){
            e.printStackTrace();
        }

        return "Inserted Row";

    }

2 Answers2

2

Well the first problem with the code is the wrong string comparison, you should use equals() method instead of == when comparing strings. See How do I compare strings in Java? for details.

Having said that your code could be highly improved as follows:

public String insert(Object obj, String idGen) {
    if ("auto".equals(idGen) || "manual".equals(idGen)) {
        try {
            entityManager.getTransaction().begin();
            entityManager.persist(obj);
            entityManager.getTransaction().commit();
            return "Inserted row";
        } catch (Exception ex) {
            e.printStackTrace();
            return String.format("Insertion failed: %s", ex.toString());
        }
    } 
    return String.format("Object not inserted because of idGen value: %s", idGen);
} 

In this way you do not create transactions at least you are completely positive you have to insert the object in database, saving the cost of transaction management. You have also tested the two positive cases in just one if instead of having an if-else if block.

Now, back to the original question, the code presented above should be enough to test if an object has been inserted on database. But you always can test it yourself with a local database connection.

Hint

Catching a generic exception is not a good practice, we should catch specific ones instead. For instance looking at EntityManager#persist() we can see that this method can throw EntityExistsException, IllegalArgumentException or TransactionRequiredException. So catching these exceptions makes much more sense than the generic Exception type.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • So there is no way to know the state of persistence thru entitymanager obj? – Ganesh Kumar Palaniappan Dec 28 '16 at 14:19
  • How did you get know that entityManager.persist() will throw those error only? where did you found the reference of it. Please share. – Ganesh Kumar Palaniappan Dec 28 '16 at 14:25
  • 1
    *How did you get know that entityManager.persist() will throw those error only?* JPA is a public and standard Java API, you can Google for EntityManager class documentation, or just click the link I've provided in my answer ;) – dic19 Dec 28 '16 at 14:27
2
entityManager.persist(obj); // here how to confirm the object was inserted or not into database

I assume you stepped over this line while debugging, and freaked out seeing no new records appear in the database.

Don't worry, the entity manager is smart enough to know it can wait with reflecting the changes in the database until you actually commit the transaction. The method EntityManager.persist(obj) only tells the entity manager that obj is now persistent - changes to its state will now be tracked, and that a new record storing its state needs to be created in the database by the end of the transaction at the latest.

At times, you need side effects of inserting data into the database to occur (e.g. you need to retrieve the autogenerated id after persisting the entity). For such use cases, you can call EntityManager.flush() to force pending changes to be persisted to the database. You'll still be able to rollback the transaction after flushing, if need be.

crizzis
  • 9,978
  • 2
  • 28
  • 47