question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

QuerySpec : query by child objects (OneToMany)

See original GitHub issue

I’ve got a @JsonApiFindAll endpoint on a findAll(QuerySpec requestParams) method which is working great for most of my needs, generally the object I am querying has single fields that I am searching on using EQ, LIKE, GT, and LT.

However, I want to query the object based on a collection of child entities and no matter what i try cannot get it to work.

Given I have entities like:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonApiResource(type = "people")
class Person {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonApiId
    private Long id;

    private String firstname;
    private String lastname;
    private String email;
}

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonApiResource(type = "article")
class Article {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonApiId
    private Long id;

    @ManyToOne @JoinColumn(name = "personId")
    @JsonApiToOne @JsonApiIncludeByDefault
    private Person author;

    @OneToMany(mappedBy = "article", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JsonApiToMany
    private Set<Comment> comments;

    private String content;
}

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonApiResource(type = "comment")
class Comment {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonApiId
    private Long id;

    @ManyToOne @JoinColumn(name = "personId")
    @JsonApiToOne @JsonApiIncludeByDefault
    private Person author;

    private String content;
}

I want to query the articles that have comments by a particular users.

On the client side I’m using Ember which uses JsonAPI but I’ve not been able to get it to call a URL in the format that Katharsis wants.

Some examples of the URLs I’ve been trying (the ‘CONTAINS’ is a custom FilterOperator):

  • articles?filter[comments][0][author][id]=85&filter[comments][1][author][id]=1310
  • articles?filter[comments][0][person][id]=85&filter[comments][1][person][id]=1310
  • articles?filter[comments][0][author][id]=85&filter[comments][1][author][id]=1310&include[articles]=comments.author
  • articles?filter[comments][CONTAINS][0][person][id]=85&filter[comments][CONTAINS][1][person][id]=112
  • articles?filter[comments][]=224&filter[comments][]=66

no matter what I try I keep getting io.katharsis.utils.parser.ParserException: Cannot parse to java.util.Set

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SingingBushcommented, Mar 10, 2017

I wasn’t actually choosing to put the comments index in there so it’s a red herring. It was just the way Ember.js was generating the query URL when I call their query function with filters, see ember docs. Probably just because I was only specifying 2 author id’s.

Essentially I would want any articles that have comments that are authored by user 85 or 1310. I suspect a URL format of articles?filter[comments][CONTAINS][author][id]=85,1310 or articles?filter[comments][author][id][IN]=85,1310 would make sense, not sure how tricky it would be to implement though.

0reactions
Ramblurrcommented, Mar 21, 2017

I agree in theory with that statement @masterspambot , but what @SingingBush is proposing isn’t supported by ember at all, specifically this:

articles?filter[comments][CONTAINS][author][id]=85,1310
Read more comments on GitHub >

github_iconTop Results From Across the Web

JPQL query for select parent Entity having @OneToMany ...
With help of @Andrey, finally I could change my repository method to : @Query(value = "select r from Recipes r " + "where...
Read more >
Querying tables and indexes: Java - Amazon DynamoDB
The Query operation enables you to query a table or a secondary index in Amazon DynamoDB. You must provide a partition key value...
Read more >
Advanced Spring Data JPA - Specifications and Querydsl
The first method simply expects to find a single customer with a given ... wrapped into a Specification object or via Querydsl predicates....
Read more >
SQL/JSON-Query — Query Specifications - GitHub Pages
Generate SQL/JSON nested data queries and matching result types in Java or TS. ... A query specification is described via the QuerySpec interface....
Read more >
crnk-io/Lobby
There are some further validation for things that should never have happened in the first place. ... PR would be welcome should you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found