Is there a way to use RequestFactory to create two entities in a single request? I tried:
EmployeeRequest request = requestFactory.employeeRequest();
EmployeeProxy newEmployee = request.create(EmployeeProxy.class);
newEmployee.setName("Joe!");
Request<Void> createReq = request.persist().using(newEmployee);
createReq.fire();
EmployeeProxy newEmployee2 = request.create(EmployeeProxy.class);
newEmployee2.setName("Sam!");
Request<Void> createReq2 = request.persist().using(newEmployee2);
createReq2.fire();
But I get an error that a request is already in progress. When I made two separate EmployeeRequests:
EmployeeRequest request = requestFactory.employeeRequest();
EmployeeProxy newEmployee = request.create(EmployeeProxy.class);
newEmployee.setName("Joe!");
Request<Void> createReq = request.persist().using(newEmployee);
createReq.fire();
EmployeeRequest request2 = requestFactory.employeeRequest();
EmployeeProxy newEmployee2 = request2.create(EmployeeProxy.class);
newEmployee2.setName("Sam!");
Request<Void> createReq2 = request2.persist().using(newEmployee2);
createReq2.fire();
Then two separate requests are made from the browser. I'm hoping that something in the RequestFactory can merge multiple requests - I have to create hundreds of entities at a time, and I don't want to make hundreds of requests!
Yes, it's possible. In your first example, just remove the line
createReq.fire();
When you call createReq2.fire()
at the end, then GWT sends both newEmployee and newEmployee2 in one single request (because they were both persisted in the context of your EmployeeRequest "request
"). I personally find the semantics a bit strange, but that's just my opinion.
Addendum by Riley: The following syntax is equivalent and is much more intuitive:
EmployeeRequest request = requestFactory.employeeRequest();
EmployeeProxy newEmployee = request.create(EmployeeProxy.class);
newEmployee.setName("Joe!");
request.persist().using(newEmployee);
EmployeeProxy newEmployee2 = request.create(EmployeeProxy.class);
newEmployee2.setName("Sam!");
request.persist().using(newEmployee2);
request.fire();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With