1

I am using following code to add an entity to datastore. However, I am getting an exception which I am not able to resolve. Any insights?

EntityManager em = EMFService.get().createEntityManager();
MyEntity temp = new MyEntity (param1, numOptions, option1, option2, option3);
em.persist(temp);
em.close(); 

I get the following exception at em.close() line in above code:

javax.persistence.PersistenceException: Cannot make object transient since object is new and not yet committed
    at org.datanucleus.api.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:302)
    at org.datanucleus.api.jpa.JPAEntityManager.close(JPAEntityManager.java:197)
    at com.vikrams.examquestweb.dao.Dao.addQuestion(Dao.java:34)

It's weird that searching for the exception description "Cannot make object transient since object is new and not yet committed" in Google gives no matching results. Am I the first one who got this. Maybe I am making some really silly mistake somewhere. Please help.

1 Answer 1

1

First of all you will also need to open a transaction to save any entity. The basic code to do this is something like this:

em.getTransaction().begin();    
em.persist(temp);
em.close(); 
em.getTransaction().commit();
em.close();

Then you should also post howr you entity looks and how it is mapped.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.