gpt4 book ai didi

android - 使用greendao for android的多对多编译错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:44:47 28 4
gpt4 key购买 nike

<分区>

问题已解决! - 我在底部添加了我的解决方案。

我认为这是一个相当简单的问题,但我似乎无法在文档中找到答案。

我正在尝试使用 greendao for 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");

错误:在 Customer.java 中:customerId 无法解析为变量在 Product.java 中:productId 无法解析为变量

请帮忙,谢谢。

编辑:

以下是 Customer.java 中的问题代码摘录(自动生成):

/** 一对一关系,在第一次访问时解决。 */

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;
}

问题:此生成的代码试图引用 customerId,但 customerId 不作为类的成员之一存在:

公共(public)类客户{

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;

解决方案:

所以我一直试图做的是建立多对多关系模型。我在做什么:客户(M:1)客户产品(1:M)产品

然而我应该做的是:客户(1:M)客户产品(M:1)产品

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com