我希望将Android ROOM应用到我的项目中。 但是我无法解决一个订单(customer_id)和客户(id)之间的一对一关系。
在OrderDao接口中,是否可能填充@Ignore protected Customer customer字段?
我只能解决Java代码。还有其他方法吗?
// Customer.java
@Entity (tableName = "t_customer")
public class Customer {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
protected String mId;
@ColumnInfo(name = "name")
protected String mName;
}
// Order.java
@Entity(tableName = "t_order",
foreignKeys = {
@ForeignKey(entity = Customer.class,
parentColumns = "id",
childColumns = "customer_id",
onDelete = SET_NULL) }
public class Order {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
protected String mId;
@ColumnInfo(name = "description")
protected String mDescription;
@ColumnInfo(name = "customer_id")
protected String mCustomerId;
@Ignore
**protected Customer customer;**
//....
}
//OrderDao.java
@Dao
public interface OrderDao {
@Query("SELECT * from t_order ORDER BY id DESC")
LiveData<List<Order>> listByIds();
@Query("SELECT * from t_order WHERE id = :id LIMIT 1")
LiveData<Order> readById(long id);
//....
}