I have Tournament entity. He have OneToOne relation with Prize entity. Prize entity have filed "amount". So if i want to do search Tournaments that have prize between some 2 values how can i do that using JHipster QueryService ?
问题:
回答1:
Based on the fact you are using QueryService, I'm assuming you enabled the Filtering
option when generating your entities. To query Tournament
entities with a Prize
amount between two values, a few things need to be added.
In TournamentQueryService
, add the following to build a specification for the prize.amount
field:
if (criteria.getPrizeAmount() != null) {
specification = specification.and(buildReferringEntitySpecification(criteria.getPrizeAmount(), Tournament_.prize, Prize_.amount));
}
In TournamentCriteria
, add a field for the prizeAmount
filter: private DoubleFilter prizeAmount;
. Also add getters and setters.
Now you can test the request through Swagger (under menu Admin->API) and filter tournaments based on the prize amount field.
If you want to make this query in the client, you need to add two parameters to the HTTP request:
prizeAmount.greaterOrEqualThan
prizeAmount.lessOrEqualThan
You can add them in the query
call like below, which will return Tournaments with prize amounts >= 1 and <= 5 (example is Angular):
loadAll() {
this.tournamentService.query({
'prizeAmount.greaterOrEqualThan': 1,
'prizeAmount.lessOrEqualThan': 5
}).subscribe(
(res: HttpResponse<ITournament[]>) => {
this.tournaments = res.body;
},
(res: HttpErrorResponse) => this.onError(res.message)
);
}
If you want the TournamentDTO
to contain the prize amount as a field, the prizeAmount
field needs to be added in the TournamentDTO
(add field and getters/setters). You also need to add @Mapping(source = "prize.amount", target = "prizeAmount")
in TournamentMapper
to map the prize data to that field in the TournamentDTO
.
回答2:
Is it possible to make that if we add another relation in Prize (Currency for example) and build specification ?
I didn't succeed because the definition is
buildReferringEntitySpecification(Filter<X> filter,
SingularAttribute<? super ENTITY, OTHER> reference,
SingularAttribute<OTHER, X> valueField)
So we have buildReferringEntitySpecification(Filter<Long> filter,
SingularAttribute<Tournament, Prize> reference,
SingularAttribute<Prize, Currency *(instead of Long)*> valueField)