首页 > 解决方案 > 具有一对多关系的房间数据库

问题描述

我有 2 个实体:员工和专业。每个员工都可以有一些专业。生成 POJO。

@Entity(indices = {@Index(value = {"f_name", "l_name", "birthday", 
"avatarLink"}, unique = false)}, inheritSuperIndices = true)
public class Employee implements Serializable {
@PrimaryKey(autoGenerate = true)
private int employeeId;
private String f_name;
private String l_name;
private String birthday;
@Ignore
private int age;
private String avatarLink;
@Embedded
private List<Specialty> specialty;
private final static long serialVersionUID = -8824149947485321362L;

@Ignore
public Employee() {
}

public Employee(int employeeId, String f_name, String l_name, String 
birthday, String avatarLink, List<Specialty> specialty) {
    this.employeeId = employeeId;
    this.f_name = f_name;
    this.l_name = l_name;
    this.birthday = birthday;
    this.avatarLink = avatarLink;
    this.specialty = specialty;
}

public int getEmployeeId() {
    return employeeId;
}

public void setEmployeeId(int employeeId) {
    this.employeeId = employeeId;
}
And some more setters\getters...

和专业

@Entity
public class Specialty implements Serializable {

@PrimaryKey
private int specId;
private String specName;
private final static long serialVersionUID = 4288061416169200241L;

public Specialty(int specId, String specName) {
    this.specId = specId;
    this.specName = specName;
}

@Ignore
public Specialty() {
}

public int getSpecId() {
    return specId;
}
    And some more setters\getters...

我有这个错误:错误:实体和 Pojos 必须有一个可用的公共构造函数。您可以有一个空的构造函数或参数与字段匹配的构造函数(按名称和类型)。

我检查了很多问题并阅读了文档,但这对我没有帮助。谢谢

标签: databaseone-to-manyandroid-roompojo

解决方案


您应该将 @Ignore 添加到 serialVersionUID 字段。在 Room 的视图中,它只是另一列。

@Ignore
private final static long serialVersionUID = 4288061416169200241L;

推荐阅读