一对多关系的存储列表使用 ObjectBox?
Store list of One-To-Many relation Using ObjectBox?
我从服务器得到如下 json:
{ "customers": [
{
"customer_name": "name1",
"orders_list": [
{
"order_id": "1111111",
"order_type": "a",
"order_date": "11/02/2020 "
},
{
"order_id": "1111112",
"order_type": "b",
"order_date": "11/02/2020 "
},
{
"order_id": "1111113",
"order_type": "c",
"order_date": "11/02/2020 "
}
]
},
//more customers
]
}
我使用 Retrofit 获取它并希望使用 Objectbox 存储它。
客户实体:
@Entity
public class Customer {
@Id long id;
@SerializedName("customer_name")
private String customer_name;
@SerializedName("array_of_matchs")
@Transient
public List<Order> orders_list;
@Backlink
public ToMany<Order> orders;
//constructor , getters , setters ,...
}
订单实体:
public class订单{
@Id
public long id;
@SerializedName("order_id")
private int order_id;
@SerializedName("order_type")
private String order_type;
@SerializedName("order_date")
private String order_date;
public ToOne<Customer> customer;
//constructor , getters , setters ,...
}
Gson 转换器 returns Customer
的 List
和他的所有 orders.I 试图存储如下客户:
customersBox.put(list_of_customers_get_from_retrofit);
和订单:
for (Customer each_customer : list_of_customers_get_from_retrofit ) {
orderBox.put(each_customer.orders_list);
}
然后当我选中 Data Browser 的复选框时,所有 customerToOneRelationId
都设置为 0 。
这有什么问题吗?
在这种情况下,我们如何在 customer
和他的 Order
s 列表之间建立关系?
如何正确存储订单?
我必须手动设置目标:
for (Customer each_customer : list_of_customers_get_from_retrofit ) {
for (Order each_order : each_customer.orders_list ) {
each_order.customer.setTarget(each_customer);
orderBox.put(each_order);
}
}
感谢这个 repository(这对像我这样的任何 Objectbox 新手都很有用)。
我从服务器得到如下 json:
{ "customers": [
{
"customer_name": "name1",
"orders_list": [
{
"order_id": "1111111",
"order_type": "a",
"order_date": "11/02/2020 "
},
{
"order_id": "1111112",
"order_type": "b",
"order_date": "11/02/2020 "
},
{
"order_id": "1111113",
"order_type": "c",
"order_date": "11/02/2020 "
}
]
},
//more customers
]
}
我使用 Retrofit 获取它并希望使用 Objectbox 存储它。
客户实体:
@Entity
public class Customer {
@Id long id;
@SerializedName("customer_name")
private String customer_name;
@SerializedName("array_of_matchs")
@Transient
public List<Order> orders_list;
@Backlink
public ToMany<Order> orders;
//constructor , getters , setters ,...
}
订单实体:
public class订单{
@Id
public long id;
@SerializedName("order_id")
private int order_id;
@SerializedName("order_type")
private String order_type;
@SerializedName("order_date")
private String order_date;
public ToOne<Customer> customer;
//constructor , getters , setters ,...
}
Gson 转换器 returns Customer
的 List
和他的所有 orders.I 试图存储如下客户:
customersBox.put(list_of_customers_get_from_retrofit);
和订单:
for (Customer each_customer : list_of_customers_get_from_retrofit ) {
orderBox.put(each_customer.orders_list);
}
然后当我选中 Data Browser 的复选框时,所有 customerToOneRelationId
都设置为 0 。
这有什么问题吗?
在这种情况下,我们如何在 customer
和他的 Order
s 列表之间建立关系?
如何正确存储订单?
我必须手动设置目标:
for (Customer each_customer : list_of_customers_get_from_retrofit ) {
for (Order each_order : each_customer.orders_list ) {
each_order.customer.setTarget(each_customer);
orderBox.put(each_order);
}
}
感谢这个 repository(这对像我这样的任何 Objectbox 新手都很有用)。