会议室数据库实体初始化

时间:2018-10-10 07:18:17

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

我们有以下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;

如何分配外键,以及表关系如何。大部分讲基础知识的教程。谢谢

1 个答案:

答案 0 :(得分:3)

  

添加foreignKeys意味着我们在该实体和   其他班级。在此参数中,我们声明parentColumns,即   用户类和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