I am trying to use GWTs RequestFactory to (at the moment) do something very simple and return a list of objects each containing some data and another object. I don't seem to be able to get my other object - instead I always get null.
My code looks something like this...
I have some UserMessage objects which each include a Message object.
UserMessage
@Entity
public class UserMessage implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Version
private Integer version = 0;
@ManyToOne
@JoinColumn(name = "messageId")
private Message message;
private String user;
private int priority;
private boolean read;
private Date expiry;
private boolean sent;
... getter/setters etc
Message
@Entity(name = "UUMessage")
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Version
private Integer version = 0;
private String title;
private String mimeType;
private String message;
private Date received;
private String fromUser;
public Message() {
}
... getter/s开发者_JS百科etters etc
each with their own proxy classes
UserMessageProxy
@ProxyFor(value = UserMessage.class, locator = UserMessageLocator.class)
public interface UserMessageProxy extends EntityProxy {
Long getId();
void setId(Long id);
MessageProxy getMessage();
void setMessage(MessageProxy message);
String getUser();
}
MessageProxy
@ProxyFor(value = Message.class, locator = MessageLocator.class)
public interface MessageProxy extends EntityProxy {
Long getId();
void setId(Long id);
String getTitle();
void setTitle(String title);
}
I have a factory and a context
@Service(value = CentralNotificationService.class, locator = CentralNotificationServiceLocator.class)
public interface CnsRequestContext extends RequestContext {
Request<List<UserMessageProxy>> getMessagesForUser(String user, int start, int length);
}
When I call getMessagesForUser(...) on the client my server side service code gets called, the entries in the database are retrieved and I get a list of UserMessageProxy returned to the client. Unfortunately calling getMessage() on any of these returns null and I can't work out why.
I am not getting any errors or warnings. On the server side I can "see" that the UserMessage does contain the Message objects when the RequestFactory code calls into my service classes.
Why are my objects null? Are there any conditions I am not satisfying?
GWT 2.4 BTW (but also had problems with 2.3)
Your code is probably missing a .with("message")
:
When querying the server, RequestFactory does not automatically populate relations in the object graph. To do this, use the with() method on a request and specify the related property name as a String
[…]
It is also necessary to use the with() method to retrieve any properties with types extending ValueProxy. The with() method takes multiple String arguments, so you can specify multiple property names at once. To specify nested properties, use dot notation.
— Source: http://code.google.com/webtoolkit/doc/latest/DevGuideRequestFactory.html#relationships
精彩评论