Room Persistence库中外键是否应具有相应的获取器和设置器?

时间:2018-08-17 23:46:42

标签: java android android-room

以下代码显示我已经创建了一个类名为UserGeneral的实体。此实体唯一的每一列都有对应的getter和setter。但是,外键没有getter和setter。 我需要为user_details_iduser_location_id提供getter和setter吗?

@Entity(
    tableName = "User_General",
    indices = {
        @Index("user_details_id"),
        @Index("user_location_id")
    },
    foreignKeys = {
        @ForeignKey(
            entity = UserDetails.class,
            parentColumns = "user_details_id",
            childColumns = "user_details_id"
        ),
        @ForeignKey(
            entity = UserLocation.class,
            parentColumns = "user_location_id",
            childColumns = "user_location_id"
        )
    }
)
public class UserGeneral {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "user_general_id")
    private int user_general_id;

    @ColumnInfo(name = "first_name")
    private String first_name;

    @ColumnInfo(name = "last_name")
    private String last_name;

    @ColumnInfo(name = "flagged_delete")
    private boolean flagged_delete;

    @ColumnInfo(name = "user_details_id")
    private int user_details_id;

    @ColumnInfo(name = "user_location_id")
    private int user_location_id;


    public int getUser_general_id() {
        return user_general_id;
    }

    public void setUser_general_id(int user_general_id) {
        this.user_general_id = user_general_id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public boolean isFlagged_delete() {
        return flagged_delete;
    }

    public void setFlagged_delete(boolean flagged_delete) {
        this.flagged_delete = flagged_delete;
    }
}

1 个答案:

答案 0 :(得分:0)

您必须提供吸气剂,否则会得到类似的东西

error: Cannot find getter for field.
    private int user_details_id;
您在构造函数中使用

设置程序或传递参数。如果您没有这样的方法,那么您将无法正确初始化该对象。在没有设置程序/构造函数的情况下,user_details_id始终为0(其他外键也是如此),因此所有UserGeneral实体都指向相同的用户详细信息/位置。