Firebase中的用户组

User被定义为:

 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被定义为:

 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; } } 

尝试保存到firebase时,会产生以下运行时错误:

 java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonMappingException at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:611) 

问题在于循环引用,还是Firebase无法以这种方式存储数据?

Solutions Collecting From Web of "Firebase中的用户组"

是的,我认为这个问题与循环引用。 哪里有循环引用,总是有序列化的问题。

正如我们所看到的,用户和组之间有双向关系。 根据官方文件,你可以改善结构:

 { "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 } }, ... } }