RequestFactory: return persisted ID in proxy after successful persist()

303 views
Skip to first unread message

RyanD

unread,
Dec 15, 2010, 12:14:10 AM12/15/10
to Google Web Toolkit
This might be better logged as a feature request, but I wanted to
check that I hadn't missed something first.

Given something like this:

PersonProxy person = context.create(PersonProxy.class);
person.setName(name);
context.persist().using(person).fire(..... etc.

In the call to:

onSuccess

person.getId() == null

Even though of course the persistence layer assigned it an ID.

Seems like it would be pretty useful to have GWT RF not necessarily
transmit every person property back to the client after a successful
persist, but certainly getting the ID back to the client would be
useful. For example, after the successful persist, let's say I want
to restfully navigate to a PersonPlace and use the person's new ID
(assigned by persistence layer) in the token?

As it is for that case, which will be pretty common in a rest style
CRUD app, I'd have to create a static method like:

createPerson(String name)

then return a PersonProxy. But that seems to defeat the purpose of
having context.persist() around for more complex objects where you
don't necessarily want to create a giant factory method.

Anything I missed, or should getting the persisted ID back to the
client be a feature request?

Thanks.

Y2i

unread,
Dec 15, 2010, 3:14:03 AM12/15/10
to Google Web Toolkit
EntityProxyChange.registerForProxyType(...) can be used to get the ID
of the persisted object.

The receiver's
EntityProxyChange.Handler<PersonProxy>.onProxyChange(EntityProxyChange<PersonProxy>
event) will be called.
event.getWriteOperation() should return PERSIST
event.getProxyId() should return the stable id of the proxy.

This should help with a restful navigation after a successful persist.

Thomas Broyer

unread,
Dec 15, 2010, 6:52:29 AM12/15/10
to google-we...@googlegroups.com

On Wednesday, December 15, 2010 9:14:03 AM UTC+1, Y2i wrote:
EntityProxyChange.registerForProxyType(...) can be used to get the ID
of the persisted object.

The receiver's
EntityProxyChange.Handler<PersonProxy>.onProxyChange(EntityProxyChange<PersonProxy>
event) will be called.
event.getWriteOperation() should return PERSIST
event.getProxyId() should return the stable id of the proxy.

This should help with a restful navigation after a successful persist.

It returns the "stable id", which you already have with person.stableId().

Thomas Broyer

unread,
Dec 15, 2010, 6:56:34 AM12/15/10
to google-we...@googlegroups.com

On Wednesday, December 15, 2010 6:14:10 AM UTC+1, RyanD wrote:
This might be better logged as a feature request, but I wanted to
check that I hadn't missed something first.

Given something like this:

PersonProxy person = context.create(PersonProxy.class);
person.setName(name);
context.persist().using(person).fire(..... etc.

In the call to:

onSuccess

person.getId() == null

Even though of course the persistence layer assigned it an ID.

Seems like it would be pretty useful to have GWT RF not necessarily
transmit every person property back to the client after a successful
persist, but certainly getting the ID back to the client would be
useful. For example, after the successful persist, let's say I want
to restfully navigate to a PersonPlace and use the person's new ID
(assigned by persistence layer) in the token?


Can't you have your persist() method return the ID, so you have it passed as argument to the onSuccess method?

RyanD

unread,
Dec 15, 2010, 10:42:59 AM12/15/10
to Google Web Toolkit
Of course! Thanks, Thomas. I keep forgetting that these aren't
"magical" methods, even persist(). Problem solved.

Y2i

unread,
Dec 15, 2010, 10:54:09 AM12/15/10
to Google Web Toolkit
> It returns the "stable id", which you already have with person.stableId().

May be you can use "stable id" to implement PersonPlace with the help
of RequestFactory.getHistoryToken(EntityProxyId<?> proxy) and
RequestFactory.getProxyId(String historyToken)?

Jake Wharton

unread,
Mar 17, 2011, 8:28:46 AM3/17/11
to google-we...@googlegroups.com
Is this possible using the editors as well?

When you flush the driver you get a RequestContext whose fire() only accepts a Receiver<Void>.

Ido

unread,
Apr 10, 2011, 12:35:58 PM4/10/11
to Google Web Toolkit
Hi,
Did you find any solution for that?
I'm facing the same problem now in which I would like to have the
persisted object's ID just after editor's save action.

Thanks a lot,
Ido

Y2i

unread,
Apr 10, 2011, 1:56:02 PM4/10/11
to google-we...@googlegroups.com
The flush returns whatever was passed to edit() so it can be cast back to the right type.  You can also keep the context that is passed to edit() in an activity and then simply re-use it.

Ido

unread,
Apr 11, 2011, 5:37:42 AM4/11/11
to google-we...@googlegroups.com
Thanks a lot Y2i, it's working
Reply all
Reply to author
Forward
0 new messages