2

I have the following Entity classes

@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;
    }
}

and

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

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

    public MultiplayerPlayerEntity() {
    }
}

and finally

@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;
    }
}

I am trying to do a search and bring back all MultiplayerGameEntity that contain a given UserEntity

in SQL terms (as this is how my brain works) I'd be JOINing MultiplayerGameEntity to MultiplayerPlayerEntity and then MultiplayerPlayerEntity to UserEntity

I've tried to do this using link() but I can't seem to work out how to link the second relationship.

code so far...

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

but this doesn't work stating

java.lang.IllegalArgumentException: Relation not part of Entity MultiplayerPlayerEntity (17)

Is it even possible to do what I'm doing, or do I need to hack this somehow?

1 Answer 1

2

I think I've got it working, by making the links chain together so the second link is to the result of the previous link (which is actually a diff QueryBuilder)

QueryBuilder<MultiplayerGameEntity> queryBuilder = box.query();
queryBuilder.link(MultiplayerGameEntity_.players).link(MultiplayerPlayerEntity_.userEntity).equal(UserEntity_.uniqueId, uniqueId);
Sign up to request clarification or add additional context in comments.

1 Comment

Yeap, that should be it! For completion an explanation: the original code did not work as the userEntity relation is not accessible directly from the MultiplayerGameEntity_ entity info. Using QueryBuilder.link(relationInfo) returns a QueryBuilder that can access relations of the target entity info relationInfo points to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.