问题
A User is defined as:
public class User {
private String email;
private String uid;
private List<Group> groups;
public User(String email, String uid) {
this.email = email;
this.uid = uid;
this.groups = new ArrayList<>();
}
public User() {}
public User(String email, String uid, ArrayList<Group> groups) {
this.email = email;
this.uid = uid;
this.groups = groups;
}
public String getEmail() {
return email;
}
public String getUid() {
return uid;
}
public List<Group> getGroups() {
return groups;
}
public void addGroup(Group group) {
if (this.groups == null) {
this.groups = new ArrayList<>();
}
this.groups.add(group);
}
}
Group is defined as:
public class Group {
private List<User> memberList;
private Group() {
}
public Group(List<User> users) {
this.memberList = users;
}
public void addMember(User member) {
this.memberList.add(member);
}
public List<User> getMemberList() {
return memberList;
}
}
When attempting to save to firebase, this gives a runtime error of:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonMappingException
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:611)
Is the issue to do with circular references or is Firebase not able to store data in this way?
回答1:
Yes I think the issue is with Circular reference. Where there is Circular reference, there is always issue with serializing it.
As we can see you have two - way relationship between Users and Groups. According to official documentation you can improve the structure as:
{
"users": {
"user1": {
"name": "User 1",
"groups": {
"group1": true,
"group2": true,
"group3": true
},
"user2": {
"name": "User 2",
"groups": {
"group2": true,
"group3": true
}
},
...
},
"groups": {
"group1": {
"name": "Group 1",
"members": {
"user1": true
},
"group2": {
"name": "Group 2",
"members": {
"user1": true,
"user2": true
},
"group3": {
"name": "Group 3",
"members": {
"user1": true,
"user2": true
}
},
...
}
}
来源:https://stackoverflow.com/questions/38213006/group-of-users-in-firebase