【问题标题】:Room database Entity initialization房间数据库实体初始化
【发布时间】:2018-10-10 07:18:17
【问题描述】:

我们有以下 Json 和模型类。如何在这个 Json 上启动 Room 实体数据库。

   {
    "ResultResponse": "Success",
    "OTPValue": "3239",
    "EmployeeInfo": [
        {
            "EmployeeCode": "EMP001",
            "Name": "Natheem",
            "ContactNo": "9952265503",
            "AlternativeContactNo": "9952265502",
            "Age": "22",
            "DOB": "1995-10-08T00:00:00",
            "ImagePath": "C:\\FaceTag\\FaceTag\\Images\\EMP001\\natheem.jpg",
            "Latitude": "1.104492000000000e+001",
            "Longitude": "7.701183000000000e+001",
            "Address1": "45, Bharathiyar Nagar",
            "Address2": "Coimbatore",
            "City": "Coimbatore",
            "State": "Tamilnadu",
            "Country": "India",
            "Zip": "641001",
            "IsSupervisor": false,
            "FormId": 0
        }
    ],
    "AdditionalField": null,
    "FieldControl": null
}

我的实体类是。

  @Entity(tableName = "tbl_device_register")
public class DeviceRegister {


    @PrimaryKey(autoGenerate = true)
    private int id;

    @SerializedName("ResultResponse")
    @Expose
    private String resultResponse;
    @SerializedName("OTPValue")
    @Expose
    private String oTPValue;
    @SerializedName("EmployeeInfo")
    @Expose
    private List<EmployeeInfo> employeeInfo = null;
    @SerializedName("AdditionalField")
    @Expose
    private Object additionalField;
    @SerializedName("FieldControl")
    @Expose
    private Object fieldControl;

如何分配外键,以及表关系。大多数教程都是关于基础知识的。谢谢

【问题讨论】:

    标签: java android mvvm android-sqlite android-room


    【解决方案1】:

    添加外键意味着我们在这个实体和 其他一些类。在这个参数中我们声明了parentColumns,也就是 来自 User 类和 childColumns 的 id 列的名称,即 Repo 类中用户 id 列的名称。

    ForeignKey结构是

    @Entity(foreignKeys =
        [
            ForeignKey(
                    entity = SOURCECLASSNAME::class,
                    parentColumns = arrayOf("id"),
                    childColumns = arrayOf("id"),
                    onDelete = ForeignKey.CASCADE
            )
        ], indices = [Index(value = "id")]
    )
    

    确保,在下面导入

    import static android.arch.persistence.room.ForeignKey.CASCADE;
    

    您可以阅读One-to-many relation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 2015-01-23
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多