Using custom class as the entity id in requestfactory framework

48 views
Skip to first unread message

Andy

unread,
Sep 2, 2011, 3:17:24 PM9/2/11
to Google Web Toolkit
Hi,

I am using mongodb via morphia, and the @Id of a collection/table in
the mapping class is a custom ObjectId class, not a primitive type. Is
is possible to use it in RequestFactory?

I tried using GWT 2.4 rc1, and I am getting the following error code
in

Caused by: java.lang.NullPointerException: null
at com.test.client.Test$1MyHandler$1.onSuccess(Test.java:189)

In the code below

final ObjectIdProxy objectIdProxy =
request.create(ObjectIdProxy.class);
objectIdProxy.set_inc(tempObjectId.get_inc());
objectIdProxy.set_machine(tempObjectId.get_machine());
objectIdProxy.set_time(tempObjectId.get_time());
request.findEmployee(objectIdProxy).fire(new
Receiver<EmployeeProxy>() {

@Override
public void onSuccess(EmployeeProxy response) {
dialogBox.setText("Remote Procedure Call");
serverResponseLabel.removeStyleName("serverResponseLabelError");
serverResponseLabel.setHTML(response.getId().get_time() + "==" +
tempObjectId.get_time() ); <------------------ null exception in
this line
dialogBox.center();
closeButton.setFocus(true);
}


it seems like the id is not transferred backed from server to client
although I am able to pass the ObjectIdProxy to the server, but when
the server return the objectId included in the Employee object, it
seems like the client side is not able to pick it up, the
response.getId() return null and hence the error, if I changed to
response.getUserName() it is working perfectly.


@Entity
public class Employee {

// just a dump that return the record
public static Employee findEmployee(ObjectId id) {
if (id == null) {
return null;
}

Employee employee = new Employee();
employee.setId(id);
employee.setDepartment("test");
employee.setUserName("Andy" + id.get_time());
employee.setVersion(System.currentTimeMillis());
return employee;

}

public void persist() {
// do nothing
}

private String userName;

private String department;

@Id
private ObjectId id;

@Version
private Long version;

.......
}

---------------------------------------------------------------------------------------------------------------------------------

@ProxyFor(com.test.server.Employee.class)
public interface EmployeeProxy extends EntityProxy {

String getDepartment();

ObjectIdProxy getId();

String getUserName();

void setDepartment(String department);

void setUserName(String userName);

EntityProxyId<EmployeeProxy> stableId();
}

---------------------------------------------------------------------------------------------------------------------------------------

ObjectId class is very long, but basically only use 3 field and we can
use it in the server perfectly.
Could you please advice what I have do wrong or if there are any
alternative ways?

Kind Regards
Andy

Thomas Broyer

unread,
Sep 2, 2011, 5:29:23 PM9/2/11
to google-we...@googlegroups.com
As with any "related object", you have to explictly ask for it in the .with() of your Request; i.e. request.findEmployee(objectIdProxy).with("id").fire(...)

Thomas Broyer

unread,
Sep 2, 2011, 5:30:46 PM9/2/11
to google-we...@googlegroups.com
To be clear: the "id" property (getId/setId on your proxy) has no special treatment, it's just a property. The ID of the object (server-side) is only used "internally" (within the stableId() of proxies).

Andy

unread,
Sep 2, 2011, 10:04:12 PM9/2/11
to Google Web Toolkit
You are awesome! Many thanks Thomas for the kind help. :)

Maxim Ivanov

unread,
Sep 4, 2011, 4:51:20 AM9/4/11
to google-we...@googlegroups.com
Hi Andy.

As I know you don't need manual proxing for ObjectId's.
You should import gwt module prepared for it specially as described here http://code.google.com/p/morphia/wiki/GoogleWebToolkit
This module provides custom serializator for mongo ObjectId - http://code.google.com/p/morphia/source/browse/trunk/gwt/src/main/java/org/bson/types/ObjectId_CustomFieldSerializer.java
Reply all
Reply to author
Forward
0 new messages