Thursday, February 24, 2011

Re: RequestFactory, JPA and sub-entity creation

Hi Tim,

It seems like you would want to do the following within the same RequestFactory service method (and probably the same JPA transaction, as well):

Persist the new Phone entity
Persist the new Person entity

Is that possible?

/dmc

On Thu, Feb 24, 2011 at 12:27 AM, Tim <tim.murison@gmail.com> wrote:
Hello all,

I'm using GWT 2.2 and I'm trying to wrap my head around a problem that
seems fundamental to the combination of RequestFactory, JPA and sub-
entity creation.

I have a Person entity with a @OneToMany list of Phone entity. In
client code, I use the editor framework to present data to the user.
If I want to create a new Phone entity for my Person, I use the
RequestContext to create a Phone proxy and attach it to the Person
proxy directly and send only the Person proxy to the server (since it
includes the new Phone proxy). On the server side, I have a method
that guarantees persist()ing the Phone entity before merge()ing the
Person entity to avoid problems with JPA.

The problem is, the server code handling this request needs to enforce
some security requirements. This involves executing a query with the
shared EntityManager (since GWT requires a single EntityManager per
request). If the query is a named query or a native query, the
EntityManager will flush before returning the result of the query.
Because GWT has already fetched the existing Person entity from the
database and added a new, not yet persisted Phone entity to the
Person, JPA fails with: "During synchronization a new object was found
through a relationship that was not marked cascade PERSIST:
Phone{id=null}."

I don't see any way to avoid this problem. RequestFactory is creating
the Person entity that my handler code is given. So far the only
solution I see is using multiple EntityManagers, one for read-write
operations, one for read-only. With that approach I run my named
queries on the read-only manager avoiding a flush on GWT-dedicated
EntityManager. I really don't like this idea, and since this seems
like a common enough setup, I'm hoping someone has encountered and
solved this problem.

Any ideas?

-Tim

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
David Chandler
Developer Programs Engineer, Google Web Toolkit
w: http://code.google.com/
b: http://googlewebtoolkit.blogspot.com/
t: @googledevtools

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment