QuerySpec : query by child objects (OneToMany)
See original GitHub issueI’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:
- Created 7 years ago
- Comments:6 (5 by maintainers)
Top GitHub Comments
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
orarticles?filter[comments][author][id][IN]=85,1310
would make sense, not sure how tricky it would be to implement though.I agree in theory with that statement @masterspambot , but what @SingingBush is proposing isn’t supported by ember at all, specifically this: