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