如何将用户表“addressId”列与地址表“id”列相关联?当我插入用户模型数据时,我如何在“用户和地址”两个表中插入数据。和获取数据的情况相同?
地址模型是用户模型的嵌套类。
@Entity(tableName = "user")
public class User {
@PrimaryKey(autoGenerate = true)
private Integer localId;
private Integer id;
private String name;
private Integer addressId;
private Address address;
}
@Entity(tableName = "address")
public class Address {
@PrimaryKey(autoGenerate = true)
private Integer localId;
private Integer id;
private String address1;
private String address2;
private String address3;
}
见https://developer.android.com/training/data-storage/room/referencing-data
基本上你的选择是:
@Embedded