Reputation: 97
we have following Json and model class. how to initiate Room entity database on this Json.
{
"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
}
and my entity class is.
@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;
How to assign the Foreign key, and what about table relationship. most of the tutorials speaking about the basics. Thanks
Upvotes: 1
Views: 566
Reputation: 75788
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.
The ForeignKey Structure is
@Entity(foreignKeys =
[
ForeignKey(
entity = SOURCECLASSNAME::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("id"),
onDelete = ForeignKey.CASCADE
)
], indices = [Index(value = "id")]
)
Make sure, import below
import static android.arch.persistence.room.ForeignKey.CASCADE;
You can read One-to-many relation
.
Upvotes: 3