在以下位置查看相关文档 https://docs.objectbox.io/queries#add-query-conditions-for-related-entities-links,并且不想在实体本身中使用任何@Backlink注释,我想从相关表中获取对象列表。
在文档的示例中,它适用于...
// get all Address objects with street "Sesame Street"...
val builder = box.query().equal(Address_.street, "Sesame Street")
// ...which are linked from a Person named "Elmo"
builder.backlink(Person_.addresses).equal(Person_.name, "Elmo")
val sesameStreetsWithElmo = builder.build().find()
我认为这是Kotlin,这不是我的强项,但是它很有意义(除了val sesameStreetsWithElmo
类型之外)
正如第一条评论所述,我假设我会返回一个Address对象的列表。但是在用自己的代码进行测试时,我得到了Person对象的列表。我期望返回的内容错了吗?我取回名为Elmo的Person对象,但是只有芝麻街上的Address对象,对吗?我只想拥有Address对象,但是却得到了一个Person列表。
如果这是正确的,那么我只是被误解了(也许更新文档来提供帮助,因为在我看来很清楚,这应该只返回Address对象)。但是,如果没有,那么也许有人可以告诉我我哪里出了问题。
(我正在我的工作机上编写此代码,无法访问我的个人项目,我可以在其中给出自己的代码示例,但如果有帮助,我可以稍后添加)
更新:我再次尝试过并提供了代码。
代码
我希望从build().find()
回到List<DailyChallengeRoundEntity>
,因为这是该框的组成部分(稍后将在更多代码示例中显示)。但是相反,它说find()返回List<DailyChallengeProgressEntity>
public List<DailyChallengeRoundEntity> getRounds(String uniqueId, String date) {
QueryBuilder<DailyChallengeRoundEntity> builder = box.query().equal(DailyChallengeRoundEntity_.date, date);
List<DailyChallengeProgessEntity> dailyChallengeProgessEntities = builder.backlink(DailyChallengeProgessEntity_.rounds).equal(DailyChallengeProgessEntity_.uniqueId, uniqueId).build().find();
}
为了展示我的盒子是如何生成的,此类中的盒子首先来自构造函数...
public DailyChallengeRoundManager(DB db) {
super(db, DailyChallengeRoundEntity.class);
}
调用
public BaseDbManager(DB db, Class<T> boxType) {
box = db.getBox(boxType);
}
DB类看起来像...
private void createMyObjectBox() throws IOException {
File objectstorefile = new File("../objectBox/objectstorefile");
if(!objectstorefile.isDirectory()) {
objectstorefile.mkdirs();
}
File boxStoreDir = objectstorefile;
if(store == null) {
store = MyObjectBox.builder().directory(boxStoreDir).build();
}
}
public<T> Box<T> getBox(Class<T> object) {
if(store == null) {
try {
createMyObjectBox();
} catch (IOException e) {
e.printStackTrace();
}
}
return store.boxFor(object);
}
和我的两个类(我没有使用@Backlink,但是文档说我没有。尽管我已经尝试过使用Annotations等的各种组合,但仍然没有用)
package uk.co.russellwheeler.db.entities;
@io.objectbox.annotation.Entity
public class DailyChallengeRoundEntity extends BaseEntity {
//fields are duplicated from parent table, but it makes it much easier to search on later
private String uniqueId;
private String date;
private int round;
private String word;
private int score;
public DailyChallengeRoundEntity() {
}
public DailyChallengeRoundEntity(String uniqueId, String date, int round, String word, int score) {
this.uniqueId = uniqueId;
this.date = date;
this.round = round;
this.word = word;
this.score = score;
}
public String getUniqueId() {
return uniqueId;
}
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getRound() {
return round;
}
public void setRound(int round) {
this.round = round;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
package uk.co.russellwheeler.db.entities;
import io.objectbox.relation.ToMany;
@io.objectbox.annotation.Entity
public class DailyChallengeProgessEntity extends BaseEntity {
private String uniqueId;
private String date;
private ToMany<DailyChallengeRoundEntity> rounds;
public DailyChallengeProgessEntity() {
}
public DailyChallengeProgessEntity(String uniqueId, String date) {
this.uniqueId = uniqueId;
this.date = date;
}
public String getUniqueId() {
return uniqueId;
}
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public ToMany<DailyChallengeRoundEntity> getRounds() {
return rounds;
}
public void setRounds(ToMany<DailyChallengeRoundEntity> rounds) {
this.rounds = rounds;
}
}
答案 0 :(得分:0)
您返回调用了Box
的{{1}}的类型。您的假设是正确的,在此示例中,您将返回query()
对象的列表。
但是,这使我想到了ObjectBox文档的问题。该文档应非常清楚所使用的Box的类型。因此,我只是将两个代码示例中使用的“框”更新为“ personBox”或“ addressBox”,以使其更加清晰。
我猜这也是在您的代码中发生的;使用类型为A的Box构建类型B的查询... (如果不是,请发布一些代码,然后我将更新此答案。)