use*_*401 6 orm android entities many-to-many greendao
问题解决了! - 我在底部添加了我的解决方案.
我认为这是一个相当简单的问题,但我似乎无法在文档中找到答案.
我正在尝试使用greendao为android建模多对多关系,但是在运行生成器项目后,我在主项目中遇到编译错误.
我的代码指定了关系和实体:
Entity customer = schema.addEntity("Customer");
customer.addIdProperty();
customer.addStringProperty("firstName").notNull();
customer.addStringProperty("lastName").notNull();
customer.addDateProperty("birthDate");
customer.addStringProperty("phoneNumber");
customer.addStringProperty("address");
customer.addStringProperty("email");
// Product
Entity product= schema.addEntity("Product");
product.addIdProperty();
product.addIntProperty("colour").notNull();
product.addIntProperty("weight").notNull();
// CustomerProduct
Entity customerProduct = schema.addEntity("CustomerProduct");
customerProduct.addIdProperty();
Property customerId = customerProduct.addLongProperty("customerId").notNull().getProperty();
customer.addToOne(customerProduct , customerId);
ToMany customerProductToCustomers = customerProduct.addToMany(customer, customerId);
customerProductToCustomers.setName("customers");
Property productId = customerProduct.addLongProperty("productId").notNull().getProperty();
product.addToOne(customerProduct , productId);
ToMany customerProductToProducts = customerProduct.addToMany(product, productId);
customerProductToProducts.setName("products");
customerProduct.addStringProperty("something");
Run Code Online (Sandbox Code Playgroud)
错误:在Customer.java中:customerId无法解析为变量在Product.java中:productId无法解析为变量
请帮忙,谢谢.
编辑:
以下是来自Customer.java(自动生成)的问题代码的摘录:
/**To-one关系,在首次访问时解决.*/
public CustomerProduct getCustomerProduct() {
if (customerProduct__resolvedKey == null || !customerProduct__resolvedKey.equals(customerId)) {
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
CustomerProductDao targetDao = daoSession.getCustomerProductDao();
customerProduct = targetDao.load(customerId);
customerProduct__resolvedKey = customerId;
}
return customerProduct ;
}
public void setCustomerProduct(CustomerProduct customerProduct ) {
if (customerProduct == null) {
throw new DaoException("To-one property 'customerId' has not-null constraint; cannot set to-one to null");
}
this.customerProduct = customerProduct;
customerId= customerProduct.getId();
customerProduct__resolvedKey = customerId;
}
Run Code Online (Sandbox Code Playgroud)
问题:这个生成的代码试图引用customerId,但customerId不存在作为该类的成员之一:
公共类客户{
private Long id;
/** Not-null value. */
private String firstName;
/** Not-null value. */
private String lastName;
private java.util.Date birthDate;
private String phoneNumber;
private String address;
private String email;
/** Used to resolve relations */
private transient DaoSession daoSession;
/** Used for active entity operations. */
private transient CustomerDao myDao;
private CustomerProduct customerProduct;
private Long customerProduct__resolvedKey;
Run Code Online (Sandbox Code Playgroud)
方案:
所以我一直想做的就是模仿多对多的关系.我在做什么:客户(M:1)CustomerProduct(1:M)产品
但是我应该做的是:客户(1:M)CustomerProduct(M:1)产品
改成。customerId= customerProduct.getId(); int customerId= customerProduct.getId();或者更好的是,只要customerProduct__resolvedKey = customerProduct.getId();假设customerProduct__resolvedKey声明正确即可。
| 归档时间: |
|
| 查看次数: |
1505 次 |
| 最近记录: |