Saturday, May 14, 2011

Trying to update a child collection the entire collection was wiped out

Problem: Trying to update a child collection the entire collection was wiped out

Solution: Call the EntityManager’s refresh() method before calling flush()

Environment Notes: Google App Engine, Datanucleus JPA, Spring MVC 3

Summary: I as working with a detached copy of the Parent entity. When I called EntityManager’s merge(Obj o) function it essentially wiped out the list because it copied in an empty list. Calling flush() essentially set the empty list in the database. Calling refresh(Obj o) before calling flush() refreshed the object with its corresponding database entries.

References:

Forum Post

http://stackoverflow.com/questions/5244238/jpa-what-is-the-behaviour-of-merge-with-lazy-initialized-collection

EntityManager API Documentation

http://download.oracle.com/javaee/6/api/javax/persistence/EntityManager.html