问题很简单。我有一个模型:
@Table(database = AppSazakDBModel.class)
@ManyToMany(referencedTable = PageDeclaration.class)
public class Category extends BaseModel {
@Column
@PrimaryKey
public int Id;
@Column
public String name;
@Column
public String bannerAddress;
@Column
public int minAppSazakVersion;
private PageDeclaration[] pageDeclarations;
public PageDeclaration[] getPageDeclarations(){
if (pageDeclarations == null || pageDeclarations.length < 1){
Category_PageDeclaration[] category_pageDeclarations = (Category_PageDeclaration[]) (new Select().from(Category_PageDeclaration.class).where(Category_PageDeclaration_Table.category_Id.eq(Id)).queryList().toArray());
ArrayList<PageDeclaration> listToReturn = new ArrayList<>();
for (Category_PageDeclaration category_pageDeclaration:category_pageDeclarations) {
listToReturn.add(category_pageDeclaration.getPageDeclaration());
}
pageDeclarations = (PageDeclaration[])(listToReturn.toArray());
}
return pageDeclarations;
}
public Category setId(int id) {
Id = id;
return this;
}
public Category setName(String name) {
this.name = name;
return this;
}
public Category setPageDeclarations(PageDeclaration[] pageDeclarations) {
this.pageDeclarations = pageDeclarations;
return this;
}
public Category setBannerAddress(String bannerAddress) {
this.bannerAddress = bannerAddress;
return this;
}
public Category setMinAppSazakVersion(int minAppSazakVersion) {
this.minAppSazakVersion = minAppSazakVersion;
return this;
}
}
和另一个模型:
@Table(database = AppSazakDBModel.class)
@ManyToMany(referencedTable = Editor.class)
public class PageDeclaration extends BaseModel {
@Column
@PrimaryKey
public int Id;
private Editor[] editors;
public Editor[] getEditors(){
if (editors == null || editors.length < 1)
editors = (Editor[])(new Select().from(Editor.class).where(PageDeclaration_Editor_Table.pageDeclaration_Id.eq(Id)).queryList().toArray());
return editors;
}
@Column
public String SampleImageAddress;
@Column
public String SampleVideoAddress;
public PageDeclaration setId(int id) {
Id = id;
return this;
}
public PageDeclaration setEditors(Editor[] editors) {
this.editors = editors;
return this;
}
public PageDeclaration setSampleImageAddress(String sampleImageAddress) {
SampleImageAddress = sampleImageAddress;
return this;
}
public PageDeclaration setSampleVideoAddress(String sampleVideoAddress) {
SampleVideoAddress = sampleVideoAddress;
return this;
}
}
如您所见,第一个定义了与第二个的多对多关系。
一切似乎都很好。初始化时我会做这样的事情:
for (Category category: initCategories) {
for (PageDeclaration pageDeclaration: category.getPageDeclarations()) {
for (Editor editor: pageDeclaration.getEditors()) {
editor.save();
PageDeclaration_Editor rel = new PageDeclaration_Editor();
rel.setEditor(editor);
rel.setPageDeclaration(pageDeclaration);
rel.save();
}
pageDeclaration.save();
Category_PageDeclaration rel = new Category_PageDeclaration();
rel.setCategory(category);
rel.setPageDeclaration(pageDeclaration);
rel.save();
}
category.save();
}
所以我们可以看到,我要么立即启动关系。
但是当我想从关系中检索数据时,
public PageDeclaration[] getPageDeclarations(){
if (pageDeclarations == null || pageDeclarations.length < 1){
Category_PageDeclaration[] category_pageDeclarations = (Category_PageDeclaration[]) (new Select().from(Category_PageDeclaration.class).where(Category_PageDeclaration_Table.category_Id.eq(Id)).queryList().toArray());
ArrayList<PageDeclaration> listToReturn = new ArrayList<>();
for (Category_PageDeclaration category_pageDeclaration:category_pageDeclarations) {
listToReturn.add(category_pageDeclaration.getPageDeclaration());
}
pageDeclarations = (PageDeclaration[])(listToReturn.toArray());
}
return pageDeclarations;
}
我得到了不相关的错误!!!错误是这样的:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.cclever.appsazak/ir.cclever.appsazak.PagesEditorActivity}: com.raizlabs.android.dbflow.structure.InvalidDBConfiguration: Model object: ir.cclever.appsazak.data.model.Category is not registered with a Database. Did you forget an annotation?
但在一线,
Category_PageDeclaration[] category_pageDeclarations = (Category_PageDeclaration[]) (new Select().from(Category_PageDeclaration.class).where(Category_PageDeclaration_Table.category_Id.eq(Id)).queryList().toArray());
当我将其替换为:
Category_PageDeclaration[] category_pageDeclarations = (Category_PageDeclaration[]) (new Select().from(Category_PageDeclaration.class).queryList().toArray());
一切正常,所以我认为问题应该是关于 where 子句。
我认为 DBFlow 开发人员应该改进有关多对多注释的文档!
知道如何解决吗???
TG。