gpt4 book ai didi

java - ObjectBox - 嵌套关系查询

转载 作者:行者123 更新时间:2023-11-30 05:18:40 25 4
gpt4 key购买 nike

我有以下Entity

@io.objectbox.annotation.Entity
public class MultiplayerGameEntity extends BaseEntity {

private int durationInHours;
private ToMany<MultiplayerPlayerEntity> players;

public MultiplayerGameEntity() {
}

public int getDurationInHours() {
return durationInHours;
}

public void setDurationInHours(int durationInHours) {
this.durationInHours = durationInHours;
}

public ToMany<MultiplayerPlayerEntity> getPlayers() {
return players;
}

public void setPlayers(ToMany<MultiplayerPlayerEntity> players) {
this.players = players;
}
}

@io.objectbox.annotation.Entity
public class MultiplayerPlayerEntity extends BaseEntity {

public ToOne<UserEntity> userEntity;
public ToMany<MultiplayerRoundEntity> rounds;

public MultiplayerPlayerEntity() {
}
}

最后

@io.objectbox.annotation.Entity
public class UserEntity extends BaseEntity {

private String uniqueId;
private String displayName;
private ToMany<UserEntity> friends;

public UserEntity() {
}

public UserEntity(String uniqueId, String displayName) {
this.uniqueId = uniqueId;
this.displayName = displayName;
}

public String getUniqueId() {
return uniqueId;
}

public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public ToMany<UserEntity> getFriends() {
return friends;
}

public void setFriends(ToMany<UserEntity> friends) {
this.friends = friends;
}
}

我正在尝试进行搜索并返回包含给定 UserEntity 的所有 MultiplayerGameEntity

用 SQL 术语(因为这就是我大脑的工作方式),我将 JOINMultiplayerGameEntityMultiplayerPlayerEntity,然后 MultiplayerPlayerEntityUserEntity

我尝试使用 link() 来做到这一点,但我似乎无法弄清楚如何链接第二个关系。

到目前为止的代码...

            queryBuilder.link(MultiplayerGameEntity_.players);
queryBuilder.link(MultiplayerPlayerEntity_.userEntity).equal(UserEntity_.uniqueId, uniqueId);
return queryBuilder.build().find();
}

但这不起作用说明

java.lang.IllegalArgumentException:关系不是实体 MultiplayerPlayerEntity 的一部分 (17)

是否有可能做我正在做的事情,或者我是否需要以某种方式破解它?

最佳答案

我想我已经成功了,通过将链接链接在一起,所以第二个链接是前一个链接的结果(实际上是一个 diff QueryBuilder)

QueryBuilder<MultiplayerGameEntity> queryBuilder = box.query();
queryBuilder.link(MultiplayerGameEntity_.players).link(MultiplayerPlayerEntity_.userEntity).equal(UserEntity_.uniqueId, uniqueId);

关于java - ObjectBox - 嵌套关系查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59912658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com