【问题标题】:Room database with one-to-much relation具有一对多关系的房间数据库
【发布时间】:2018-08-05 19:21:48
【问题描述】:

我有 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 必须有一个可用的公共构造函数。你可以有一个空的构造函数或一个参数匹配字段(按名称和类型)的构造函数。

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

【问题讨论】:

    标签: database one-to-many android-room pojo


    【解决方案1】:

    您应该将@Ignore 添加到serialVersionUID 字段。 在 Room 看来,它只是另一列。

    @Ignore
    private final static long serialVersionUID = 4288061416169200241L;
    

    【讨论】:

    • 你几乎是对的。事实证明我需要添加@Ignore List&lt;Specialty&gt; specialty
    猜你喜欢
    • 2018-06-13
    • 2020-03-10
    • 1970-01-01
    • 2021-12-31
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多