【发布时间】:2019-07-21 05:54:54
【问题描述】:
在文档中查看关系 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,但文档说我不必。虽然我尝试了使用注释等的各种组合,但仍然没有奏效)
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;
}
}
【问题讨论】:
标签: objectbox