firebase中的用户组

时间:2016-07-05 21:24:50

标签: java android firebase firebase-realtime-database

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是否无法以这种方式存储数据?

1 个答案:

答案 0 :(得分:6)

是的我认为这个问题与循环参考有关。如果存在循环引用,则始终存在序列化问题。

我们可以看到用户和群组之间存在双向关系。根据{{​​3}},您可以改进结构:

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