房间数据库实体初始化

Room database Entity initialization

我们有以下 Json 和模型 class。如何在此 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
}

而我的实体 class 是。

  @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;

如何分配外键,table关系如何。大多数教程都在谈论基础知识。谢谢

Adding foreignKeys means we create connection between this entity and some other class. In this parameter we declare parentColumns, which is name of the id column from User class and childColumns, which is the name of the user id column in Repo class.

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