Many-to-many compilation error using greendao for Android

The problem is solved! . I added my solution below.

I have a question, which is a pretty simple question, but I cannot find the answer in the documentation.

I am trying to model many-to-many relationships using greendao for android, however I get a compilation error in the main project after running the generator project.

My code that defines 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"); 

Errors: In Customer.java: customerId cannot be resolved by variable In Product.java: productId cannot be resolved by variable

Please help, thanks.

Edit:

Here is an excerpt with the problem code from Customer.java ( automatically generated ):

/ ** One relationship allowed 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 tries to reference customerId, but clientId does not exist as one of the members of the class:

Open Class Client {

 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; 

Decision

What I tried to do all the time was a many-to-many relationship model. What I did: Customer (M: 1) Product CustomerProduct (1: M)

However, what I had to do: Customer (1: M) CustomerProduct (M: 1) Product

+6
source share
1 answer

Change customerId= customerProduct.getId(); on int customerId= customerProduct.getId(); . or better yet, just customerProduct__resolvedKey = customerProduct.getId(); , assuming customerProduct__resolvedKey correctly declared.

0
source

All Articles