使用Room持久性库在jsonobject内表示多个jsonarray

时间:2017-11-03 09:22:40

标签: android-room

实际上我很清楚如何使用内部jsonArray和Room持久性库。 POJO课程如下:

@Entity(tableName = "customer")
public class CustomerResponseModel {
    @PrimaryKey
    @ColumnInfo(name = "version")
    @SerializedName("version")
    private int version;
    @ColumnInfo(name = "sync_date")
    @SerializedName("sync_date")
    private long syncDate;
    @Relation(parentColumn = "version", entityColumn = "customerId", entity = CustomerList.class)
    private List<CustomerList> customerList;
}

@Entity
public class CustomerList {
    @PrimaryKey
    @SerializedName("customer_id")
    private String customerId;
    @SerializedName("customer_type")
    private String customerType;
    @SerializedName("customer_name")
    private String customerName;
    @SerializedName("customer_addresses")
    @Relation(parentColumn = "customerId", entityColumn = "id", entity = CustomerAddress.class)
    private List<CustomerAddress> customerAddresses;
}

@Entity
public class CustomerAddress {
    @PrimaryKey(autoGenerate = true)
    private int id;
    @SerializedName("address")
    private String address;
    @SerializedName("contact")
    private String contact;
}
  

错误:(15,8)错误:实体不能有关系。

1 个答案:

答案 0 :(得分:0)

出现此问题是因为CustomerList类是@Entity,并且在其中使用了@Relation注释。如果你将从该类中删除@Entity,问题就会消失。

查看here

中的官方文档
  

请注意,@ Relation注释只能在Pojo类中使用,Entity类不能有关系。