3

The question may seems stupid, but for me a cycle reference is for example the object A refers an object B AND the object B refers the object A.

I am working with on a android application communicating with a GAE server with objectify DB.

My model is quite simple but I get a error:

org.codehaus.jackson.map.JsonMappingException: Direct self-reference leading to cycle (through reference chain: java.util.ArrayList[0]->com.my.model.MyMessage["senderKey"]->com.googlecode.objectify.Key["root"])

Here is my model: a MyMessage refers a MyUser (the MyUser DOESNT refer a MyMessage...

Here is the code:

public class MyMessage implements Serializable {
private static final long serialVersionUID = -1075184303389185795L;

@Id
private Long id;

@Unindexed
private String sendMessage;

@Unindexed
private String answerMessage;

private MessageStatus status = MessageStatus.FREE;

@Parent
Key<MyUser> senderKey;

Key<MyUser> answererKey;

@SuppressWarnings("unused")
private MyMessage() {
}

public MyMessage(MyUser user, String message) {
    super();
    this.sendMessage = message;
    this.senderKey = new Key<MyUser>(MyUser.class, user.getId());
}

[... getters and setters ...]
}

.

public class MyUser implements Serializable {

private static final long serialVersionUID = 7390103290165670089L;
@Id private String id;

@SuppressWarnings("unused")
private MyUser() {
    this.setId("default");
}

public MyUser(String mail) {
    this.setId(mail);
}

public void setId(String mail) {
    this.id = mail;
}

public String getId() {
    return id;
}

}

So what is exactly a Direct self-reference ?? What is wrong with my model??

Thank you.

4

1 回答 1

5

Key internally contains reference to parent Key, this is type-wise a reference to iteslf, i.e. a direct self-reference. This could potentially lead to endless loop, so Jackson is throwing an error.

Bottom line: Key is not serializable out-of-the-box. You might get by by writing a custom Jackson serializer/deserializer.

于 2012-08-14T19:17:47.860 回答