[英]JHipster Search entity by field of his entity
I have Tournament entity.我有锦标赛实体。 He have OneToOne relation with Prize entity.
他与 Prize 实体有 OneToOne 关系。 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 ?
因此,如果我想在两个值之间进行搜索锦标赛,我该如何使用 JHipster QueryService 来做到这一点?
Based on the fact you are using QueryService, I'm assuming you enabled the Filtering
option when generating your entities.基于您使用 QueryService 的事实,我假设您在生成实体时启用了
Filtering
选项。 To query Tournament
entities with a Prize
amount between two values, a few things need to be added.要查询
Prize
金额介于两个值之间的Tournament
实体,需要添加一些内容。
In TournamentQueryService
, add the following to build a specification for the prize.amount
field:在
TournamentQueryService
,添加以下内容以构建prize.amount
字段的规范:
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;
在
TournamentCriteria
,为prizeAmount
过滤器添加一个字段: private DoubleFilter prizeAmount;
. . Also add getters and setters.
还要添加 getter 和 setter。
Now you can test the request through Swagger (under menu Admin->API) and filter tournaments based on the prize amount field.现在您可以通过 Swagger(在菜单 Admin->API 下)测试请求并根据奖金金额字段过滤锦标赛。
If you want to make this query in the client, you need to add two parameters to the HTTP request:如果要在客户端进行这个查询,需要在HTTP请求中添加两个参数:
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):您可以将它们添加到如下所示的
query
调用中,这将返回奖金金额 >= 1 和 <= 5 的锦标赛(例如 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).如果您希望
TournamentDTO
包含奖金金额作为字段,则需要在TournamentDTO
添加prizeAmount
字段(添加字段和 getter/setter)。 You also need to add @Mapping(source = "prize.amount", target = "prizeAmount")
in TournamentMapper
to map the prize data to that field in the TournamentDTO
.您还需要添加
@Mapping(source = "prize.amount", target = "prizeAmount")
在TournamentMapper
的奖励数据映射到该字段TournamentDTO
。
Is it possible to make that if we add another relation in Prize (Currency for example) and build specification ?如果我们在 Prize(例如货币)中添加另一个关系并构建规范,是否可以做到这一点?
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)
所以我们有
buildReferringEntitySpecification(Filter<Long> filter, SingularAttribute<Tournament, Prize> reference, SingularAttribute<Prize, Currency *(instead of Long)*> valueField)
Please implement this method in your own class like below.请在您自己的类中实现此方法,如下所示。
public static <ENTITY, ENTITY2> Specification<ENTITY> buildReferringEntitySpecification(
Filter<Currency> filter,
SingularAttribute<? super ENTITY, ENTITY2> reference,
SingularAttribute<ENTITY2, Currency> valueField) {
if (filter.getEquals() != null) {
return buildEqualsJoin(filter.getEquals(), reference, valueField);
} else if (filter.getIn() != null) {
return buildInJoin(filter.getIn(), reference, valueField);
} else if (filter.getSpecified() != null) {
return buildByFieldSpecifiedJoin(filter.getSpecified(), reference, valueField);
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.