問題が解決しました!- ソリューションを一番下に追加しました。
かなり単純な質問だと思いますが、ドキュメントで答えが見つからないようです。
Android 用の greendao を使用して多対多の関係をモデル化しようとしていますが、ジェネレーター プロジェクトを実行した後、メイン プロジェクトでコンパイル エラーが発生します。
関係とエンティティを指定する私のコード:
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 (自動生成)からの問題コードを抜粋したものです。
/** 1 対 1 の関係で、最初のアクセスで解決されます。*/
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 はクラスのメンバーの 1 つとして存在しません:
パブリッククラスの顧客{
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;
解決策:
私がずっとやろうとしていたことは、多対多の関係をモデル化することでした。私がしていたこと: Customer (M:1) CustomerProduct (1:M) Product
しかし、私がすべきだったこと: Customer (1:M) CustomerProduct (M:1) Product