6

Problem solved! - I added my solution at the bottom.

I have what a think is a fairly simple question but I can't seem to find the answer in the documentation.

I am trying to model a many-to-many relationship using greendao for android, however I get a compile error in the main project after I run the generator project.

My code that specifies relationships and entities:

    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");

The errors: In Customer.java : customerId cannot be resolved to a variable In Product.java : productId cannot be resolved to a variable

Please help, Thanks.

Edit:

Here is an extract with the problem code from Customer.java (auto-generated):

/** To-one relationship, resolved on first access. */

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

Problem: this generated code is trying to reference customerId, but customerId doesn't exist as one of the members of the class:

public class Customer{

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;

Solution:

So what I was trying to do all along was model a many-to-many relationship. What I was doing: Customer (M:1) CustomerProduct (1:M) Product

However what I should have done: Customer (1:M) CustomerProduct (M:1) Product

1 Answers1

0

Change customerId= customerProduct.getId(); to int customerId= customerProduct.getId();. or better yet, just do customerProduct__resolvedKey = customerProduct.getId(); assuming that customerProduct__resolvedKey is correctly declared.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • This seems like it would work in setCustomerProduct function, however I am not sure if the same approach will work for getCustomerProduct() function. I think there might be a bigger issues here, possibly with the way I am defining my many-to-many entity relationships? .. since this code is auto generated.. ? – user2031401 Mar 12 '13 at 17:32
  • @user I have never used greendao, so I'm unsure about how it generates Java code. I was simply answering based on the compiler errors. I suggest that you create a small example that recreates the issue and post the code which you wrote, not the generated code. – Code-Apprentice Mar 12 '13 at 17:38
  • Thanks for your input. The first code sample is the code that I wrote, it compiles and runs fine. By running this piece of code (expert) it should automatically generate entities and their corresponding relationships. However the auto generated code has a compile error .. – user2031401 Mar 12 '13 at 20:33
  • @user2031401 Where does the generated code come from? – Code-Apprentice Mar 12 '13 at 22:39
  • The generated code is generated by the greendao framework based on the relationship and entities I specify. Thank you for your help but I think the solution to this problem requires knowledge of how to work with greendao – user2031401 Mar 12 '13 at 22:49
  • I think you are correct that someone with knowledge of greendao will be able to help you more than I can. I have one more question: what is the code where you specify the relationships? – Code-Apprentice Mar 12 '13 at 22:53
  • The first block of code, where I added a comment in bold letters. – user2031401 Mar 13 '13 at 02:01