问题
I'm trying to get the hang of the new RequestFactory API and having a really tough time of it.
My domain models consist of a Staffer
, a Person
and an Office
. Staffer has a Person and an Office as fields.
When I try to save updates to a Staffer instance in GWT, on the server-side persist()
call I see the updates in its primitive/String fields, but I do not see updates to the attached Person
or Office
objects. Here is how I'm affecting the edits on the GWT side:
private void persistStafferDetails()
{
CRMRequestFactory.StafferRequest stafferRequest = requestFactory.stafferRequest();
staffer = stafferRequest.edit(staffer);
PersonProxy person = staffer.getPerson();
person.setFirstName(firstNameField.getText());
person.setLastName(lastNameField.getText());
staffer.setPersonalEmail(personalEmailField.getText());
staffer.getHomeLocation().setStreetAddress(addressField1.getText());
staffer.getHomeLocation().setCity(cityField.getText());
staffer.getHomeLocation().setPostalCode(postalField.getText());
staffer.getHomeLocation().setProvince(provinceDropDown.getValue(provinceDropDown.getSelectedIndex()));
stafferRequest.persist().using(staffer).fire();
}
Here are the proxies:
@ProxyFor(Staffer.class)
public interface StafferProxy extends EntityProxy
{
Long getId();
PersonProxy getPerson();
void setPerson(PersonProxy person);
OfficeProxy getOffice();
void setOffice(OfficeProxy office);
String getHomePhone();
void setHomePhone(String homePhone);
String getCellPhone();
void setCellPhone(String cellPhone);
String getPersonalEmail();
void setPersonalEmail(String personalEmail);
LocationProxy getHomeLocation();
void setHomeLocation(LocationProxy homeLocation);
}
@ProxyFor(Person.class)
public interface PersonProxy extends EntityProxy
{
Long getId();
void setId(Long id);
String getFirstName();
void setFirstName(String firstName);
String getLastName();
void setLastName(String lastName);
}
@ProxyFor(Office.class)
public interface OfficeProxy extends EntityProxy
{
Long getId();
String getName();
void setName(String name);
}
And my CRMRequestFactory looks like:
public interface CRMRequestFactory extends RequestFactory
{
@Service(Staffer.class)
public interface StafferRequest extends RequestContext
{
InstanceRequest<StafferProxy, Void> persist();
Request<List<StafferProxy>> getAll();
Request<StafferProxy> findStaffer(Long id);
}
public StafferRequest stafferRequest();
@Service(Person.class)
public interface PersonRequest extends RequestContext
{
InstanceRequest<PersonProxy, Void> persist();
Request<List<PersonProxy>> getAll();
Request<PersonProxy> findPerson(Long id);
}
public PersonRequest personRequest();
@Service(Office.class)
public interface OfficeRequest extends RequestContext
{
InstanceRequest<OfficeProxy, Void> persist();
Request<List<OfficeProxy>> getAll();
Request<OfficeProxy> findOffice(Long id);
}
public OfficeRequest officeRequest();
}
回答1:
RequestFactory doesn't treat the persist()
method as anything special, so you have to implement chained persists on your own or configure your ORM system to do it for you. Another thing to check is that the findPerson()
and findOffice()
methods return the same object instance of the Person or Office object if called more than once. If you use the same EntityManager
(or your system's equivalent) throughout the lifetime of the incoming HTTP request, that usually takes care of the "missing updates" problem with non-trivial payload graphs.
A blog post about chained persistence and an issue tracker link with a short discussion.
If this doesn't help, could you post an example of your domain objects' findFoo()
and persist()
methods?
来源:https://stackoverflow.com/questions/4973440/gwt-requestfactory-not-persisting-attached-entities