Appengine JPA persist data problem

There is sometime coding JPA persist data problem may occured.

For example, I have a code that is about:

public class MemberBean {

    public void registerMember(Members member) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            em.persist(member);
            //em.refresh(member);
        } finally {
            em.close();
        }
    }

As you can see that, if you run this code, sometimes you will got:

Problem accessing /fyhaosecs. Reason:

    Illegal argument
Caused by:
javax.persistence.PersistenceException: Illegal argument
	at org.datanucleus.jpa.NucleusJPAHelper.getJPAExceptionForJDOException(NucleusJPAHelper.java:214)
	at org.datanucleus.jpa.EntityManagerImpl.close(EntityManagerImpl.java:157)
	at org.datanucleus.store.appengine.jpa.DatastoreEntityManager.close(DatastoreEntityManager.java:54)
	at com.fyhao.secs.MemberBean.registerMember(MemberBean.java:18)
	at com.fyhao.secs.FyhaosecsServlet.doGet(FyhaosecsServlet.java:24)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:693)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)

Solution:

The solution is: just add em.refresh(member); after you persist.

You can also refer to this link: http://www.datanucleus.org/products/accessplatform_1_1/jpa/em.html

Author: fyhao

Jebsen & Jessen Comms Singapore INTI University College Bsc (Hon) of Computer Science, Coventry University

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.