6

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

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

我正在尝试使用 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 不作为该类的成员之一存在:

公共类客户{

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) CustomerProduct (1:M) 产品

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

4

1 回答 1

0

更改customerId= customerProduct.getId();int customerId= customerProduct.getId();。或者更好的是,只需customerProduct__resolvedKey = customerProduct.getId();假设customerProduct__resolvedKey正确声明即可。

于 2013-03-12T04:04:45.793 回答