我正在为项目使用JPA HIBERNATE H2数据库。
当前,我正在尝试使用部分谓词(可选)来构建谓词,直到我必须创建一些复杂的查询为止,一切工作都很好。
我想将我的产品过滤到具有默认数量小于其最大数量的成分成分的产品
我写了这样的函数:
private BooleanExpression getBooleanPredicateComposition(ListPath<Product.Composition.Component, QProduct_Composition_Component> component, QProduct_Composition_Component qpath, Boolean params) {
if(null == params) {
return null;
}
BooleanExpression be = qpath.defaultQuantity.lt(qpath.maxQuantity);
return (params ? be : component.size().gt(0).not());
}
因此,将执行以下sql:
select
1
from
product$composition product_co3_
inner join
product$composition_component component4_
on product_co3_.composition_id=component4_.product$composition_composition_id
inner join
product$composition_component component5_
on product_co3_.composition_id=component5_.product$composition_composition_id
where
product_co3_.composition_id=product0_.composition_composition_id
and product_co3_.composition_id=product0_.composition_composition_id
and component4_.default_quantity<component5_.max_quantity
但是我想要一个SQL,其中component4_default_quantity与它自己的max_quantity有关,而不是与我目前拥有的另一个联接表有关。
我当然不是SQL专家,实际上我对此经验很少,但我认为应该是这样的:
select
1
from
product$composition product_co3_
inner join
product$composition_component component4_
on product_co3_.composition_id=component4_.product$composition_composition_id
where
product_co3_.composition_id=product0_.composition_composition_id
and product_co3_.composition_id=product0_.composition_composition_id
and component4_.default_quantity<component4_.max_quantity
那我应该怎么编码呢?
我尝试了许多不同的技术,例如建立新的JPAExpression或引用QProduct_Composition_Component.component的基类,但是似乎没有一种对我有用。.
提前谢谢!
我调用此函数的其余代码
public Predicate getPredicate(ComplexProduct complexProduct) {
return new BooleanBuilder()
.and(PRODUCT.country.eq(complexProduct.getCountry()))
.and(PRODUCT.storeId.eq(complexProduct.getStore()))
.and(getPartialPredicate(complexProduct).build());
}
private WhereClauseBuilder getPartialPredicate(ComplexProduct complexProduct) {
WhereClauseBuilder whereClauseBuilder = new WhereClauseBuilder()
.optionalAnd(PRODUCT.onOutage::eq,complexProduct.getOnOutage())
.optionalAnd(getBooleanPredicateZeroPriced(PRODUCT.priceList.priceTag.pricing.any().price,complexProduct.getZeroPriced(), complexProduct.getNullPriceAllowed()))
.optionalAnd(getBooleanPredicateSalable(PRODUCT.salable, complexProduct.getSalable()))
.optionalAnd(getBooleanPredicateIsActive(PRODUCT.isActive, complexProduct.getIsActive()))
.optionalAnd(getBooleanPredicateHasPromotionTimeRestriction(PRODUCT.promotion.timeRestrictions, complexProduct.getHasPromotionTimeRestriction()))
.optionalAnd(getBooleanPredicateString(PRODUCT.grillGroup, complexProduct.getGrillGroup()))
.optionalAnd(getBooleanPredicateExistsAndHasElement(PRODUCT.canAdds.component, complexProduct.getGrilledCanAdd()))
.optionalAnd(getBooleanPredicateExistsAndHasElement(PRODUCT.comments.component, complexProduct.getGrilledComment()))
.optionalAnd(getBooleanPredicateExistsAndHasElement(PRODUCT.choices.component, complexProduct.getGrilledChoice()))
.optionalAnd(getBooleanPredicateComposition(PRODUCT.composition.component, PRODUCT.composition.component.any(), complexProduct.getCompositionNeeded()))
.optionalAnd(getBooleanPredicateAvailability(PRODUCT, complexProduct.getTime(), complexProduct.getTime(), complexProduct.getDayName(), complexProduct.getAvailableAt()))
.optionalAnd(getBooleanPredicateCustomParameter(PRODUCT.customParameters, "nutritionalInfoEnergy",complexProduct.getNutritionalInfoEnergy()));
if(complexProduct.getDistributions() != null) {
addDistributionFilters(whereClauseBuilder, complexProduct.getDistributions().stream().map(distribution -> distribution.name()).collect(Collectors.toList()));
}
WhereClauseBuilder partialWhere = null;
if(complexProduct.getType() != null) {
if(complexProduct.getType() != null) {
if (partialWhere == null) {
partialWhere = new WhereClauseBuilder();
}
partialWhere = partialWhere.optionalOr(PRODUCT.productClass::eq, complexProduct.getType().getName());
}
}
if(partialWhere != null) {
whereClauseBuilder.optionalAnd(partialWhere.build());
}
return whereClauseBuilder;
}