Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a PropertyQuery on an @Id field #1028

Closed
RobbWatershed opened this issue Nov 23, 2021 · 9 comments
Closed

Using a PropertyQuery on an @Id field #1028

RobbWatershed opened this issue Nov 23, 2021 · 9 comments
Labels
bug Something isn't working
Milestone

Comments

@RobbWatershed
Copy link

RobbWatershed commented Nov 23, 2021

Using ObjectBox v3.0.1

As I'm building rather complex queries, I sometimes need to extract IDs to intersect them using Java streams & collections.

I'm trying to do that using PropertyQueries, e.g.

QueryBuilder<GroupItem> customGContentQB = store.boxFor(GroupItem.class).query();
customGContentQB.link(GroupItem_.group).equal(Group_.grouping, Grouping.CUSTOM.getId());
return customGContentQB.build().property(GroupItem_.contentId).findLongs();

Where GroupItem_.contentId is inside a ToOne relation

@Entity
public class GroupItem {

    @Id
    public long id;
    public ToOne<Content> content;
    public ToOne<Group> group;
    public int order;
}

When doing that, ObjectBox crashes with the following message

    Process: me.myapp.debug, PID: 6964
    java.lang.IllegalArgumentException: Property "contentId" is of type Relation, but we expected a property of type Long in this context.
        at io.objectbox.query.PropertyQuery.nativeFindLongs(Native Method)
        at io.objectbox.query.PropertyQuery.lambda$findLongs$1(PropertyQuery.java:209)
        at io.objectbox.query.PropertyQuery.$r8$lambda$_kSlTz290CY5pQO1ceZRF6rRKRU(Unknown Source:0)
        at io.objectbox.query.PropertyQuery$$ExternalSyntheticLambda1.call(Unknown Source:2)
        at io.objectbox.BoxStore.callInReadTx(BoxStore.java:936)
        at io.objectbox.BoxStore.callInReadTxWithRetry(BoxStore.java:882)
        at io.objectbox.query.Query.callInReadTx(Query.java:294)
        at io.objectbox.query.PropertyQuery.findLongs(PropertyQuery.java:208)
        at me.myapp.database.ObjectBoxDB.selectCustomGroupedContent(ObjectBoxDB.java:1504)

As that case isn't documented, I wondered if it is a bug, uncharted territory, or if it could get a fix.

For instance I wouldn't mind using something like PropertyQuery.findTargetIds instead of PropertyQuery.findLongs if it could make its way to the proper result.

If it is not supported at all, could you please make the message more explicit (e.g. "Querying relation Ids with a PropertyQuery is not supported") ?


PS : Current workaround I found is to code my query the other way around, but I hate to use these double links

QueryBuilder<Content> customContentQB = store.boxFor(Content.class).query();
customContentQB.link(Content_.groupItems).link(GroupItem_.group).equal(Group_.grouping, Grouping.CUSTOM.getId());
return customContentQB.build().property(Content_.id).findLongs();
@RobbWatershed RobbWatershed added the bug Something isn't working label Nov 23, 2021
@greenrobot-team
Copy link
Member

There is findIds() for a regular query, does that work for you (e.g. re-write the query to return Content objects, but then use findIds() instead of find())?

@greenrobot-team greenrobot-team added the more info required Further information is requested label Nov 29, 2021
@no-response
Copy link

no-response bot commented Dec 20, 2021

Without additional information, we are unfortunately not sure how to resolve this issue. Therefore this issue has been automatically closed. Feel free to comment with additional details and we can re-open this issue.

@no-response no-response bot closed this as completed Dec 20, 2021
@RobbWatershed
Copy link
Author

RobbWatershed commented Oct 26, 2022

First of all, sorry for the delay.

There is findIds() for a regular query, does that work for you (e.g. re-write the query to return Content objects, but then use findIds() instead of find())?

Do you mean something like that ?

builder.link(GroupItem_.content).build().findIds()

... because when I do so, ObjectBox tells me

java.lang.IllegalStateException: This call is not supported on sub query builders (links)
        at io.objectbox.query.QueryBuilder.verifyNotSubQuery(QueryBuilder.java:277)
        at io.objectbox.query.QueryBuilder.build(QueryBuilder.java:263)

I'm ending up either coding my query the other way around, when there's a @Backlink available, or streaming entire objects to extract IDs and build an array with them. Neither of these solutions are performant ☹️

Really, having a specific PropertyQuery accessor for IDs would be much simpler and efficient to use.

@RobbWatershed
Copy link
Author

@greenrobot-team please consider reopening that one. Thanks in advance

@greenrobot
Copy link
Member

Property "contentId" is of type Relation, but we expected a property of type Long in this context.
        at io.objectbox.query.PropertyQuery.nativeFindLongs(Native Method)

This actually looks like something that makes sense to allow...

@greenrobot greenrobot reopened this Oct 26, 2022
@greenrobot
Copy link
Member

Relation properties are now allowed for PropertyQuery using long internally. This will be part of the next release (no date yet).

@greenrobot greenrobot removed the more info required Further information is requested label Oct 26, 2022
@RobbWatershed
Copy link
Author

Fan-tas-tic ! Thanks @greenrobot

@greenrobot-team greenrobot-team added this to the 3.4.1 milestone Oct 31, 2022
@greenrobot-team
Copy link
Member

Also closing as fixed, will ship with the next release.

@greenrobot-team
Copy link
Member

Fix now available with release 3.5.0! Let us know if there are issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants