Я хочу выбрать некоторые из столбцов, используя множественный выбор спецификации Spring jpa, но всегда получаю все столбцы, поэтому не работает только группировка по.
Нужны только столбцы, указанные в query.multiselect, которые должны возвращать мою группу, и они будут повреждены. Но смотрите в выходной части ниже, он выбирает все столбцы.
Specification<MasterTenantParamValue> mstParamValueSpecification = new Specification<MasterTenantParamValue>() {
private static final long serialVersionUID = 1L;
@Override
public Predicate toPredicate(Root<MasterTenantParamValue> mstParamValue, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<ParameterDTO> dependsOn = searchDTO.getDependsOn();
List<DependantValue> mstDependantValues = mstparamDependencies.get(parameterId);
//Find the pipe line params from local cache (mstDependantValues) and if present then add to main dependsOn list
if (mstDependantValues != null && !mstDependantValues.isEmpty()) {
List<String> pipelineParamIds = mstDependantValues.stream()
.filter(dependantValue -> dependantValue.getPipeline()).map(DependantValue::getParamId)
.collect(Collectors.toList());
if (pipelineParamIds != null && !pipelineParamIds.isEmpty()) {
if (dependsOn == null || dependsOn.isEmpty()) {
dependsOn = new ArrayList<ParameterDTO>();
}
for (String pipelineParamId : pipelineParamIds) {
ParameterDTO dto = new ParameterDTO();
dto.setParamId(pipelineParamId);
dependsOn.add(dto);
}
}
}
List<Predicate> predicates = new ArrayList<Predicate>();
query.multiselect(mstParamValue.get("id"),
mstParamValue.get("valueName"),
mstParamValue.get("paramId"))
.groupBy(mstParamValue.get("id"),
mstParamValue.get("valueName"),
mstParamValue.get("paramId")
);
query.having(criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.count(mstParamValue), 1L));
//query.distinct(true);
predicates.add(criteriaBuilder.equal(mstParamValue.get("tenantId"), requestInfo.getTenant()));
predicates.add(criteriaBuilder.equal(mstParamValue.get("paramId"), parameterId));
//predicates.add(criteriaBuilder.like(mstParamValue.get("valueName"), "%" + search + "%"));
predicates.add(criteriaBuilder.like(criteriaBuilder.lower(mstParamValue.get("valueName")), "%" + search + "%"));
for(ParameterDTO parameterDTO : dependsOn) {
if(mstDependantValues != null) {
//DependantValue mstDependantValue = mstDependantValues.get(index);
DependantValue mstDependantValue = mstDependantValues.stream()
.filter(dependantValue -> parameterDTO.getParamId().equals(dependantValue.getParamId()))
.findAny()
.orElse(null);
List<String> valueIds = new ArrayList<>();
if(mstDependantValue != null) {
int attribute = mstDependantValue.getAttribute();
if(mstDependantValue.getPipeline() != null && mstDependantValue.getPipeline()) {
valueIds = pipelineParamSelectionRepository.findParamValuesByParamId(requestInfo.getTenant(),mstDependantValue.getParamId());
} else {
valueIds = parameterDTO.getValues().stream()
.map(ParameterValue::getValueId)
.collect(Collectors.toList());
}
Expression<String> exp = null;
if(valueIds != null && !valueIds.isEmpty()) {
if(attribute == 1) {
exp = mstParamValue.get("attr1");
predicates.add(exp.in(valueIds));
}
else if(attribute == 2) {
exp = mstParamValue.get("attr2");
predicates.add(exp.in(valueIds));
}
else if(attribute == 3) {
exp = mstParamValue.get("attr3");
predicates.add(exp.in(valueIds));
}
}
} else {
throw new RuntimeException("Not Valid Parameter Id = "+parameterDTO.getParamId());
}
}
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
boolean more = false;
List<MasterTenantParamValue> records = null;
pageIndexStartingAtOne = (pageIndexStartingAtOne == null || pageIndexStartingAtOne <= 0)? 1 : pageIndexStartingAtOne;
Integer zeroBasedPageIndex = pageIndexStartingAtOne - 1;
Pageable pageable = PageRequest.of(zeroBasedPageIndex, pageSize,Sort.by("valueName").ascending());
//results = masterTenantParamValueRepository.findAll(mstParamValueSpecification);
Page<MasterTenantParamValue> pageResult = masterTenantParamValueRepository.findAll(mstParamValueSpecification,pageable);
Вывод SQL -
Спящий режим:
select
*
from
( select
mastertena0_.surrogate_key as surrogate_key1_31_,
mastertena0_.attr1 as attr2_31_,
mastertena0_.attr2 as attr3_31_,
mastertena0_.attr3 as attr4_31_,
mastertena0_.attr4 as attr5_31_,
mastertena0_.attr5 as attr6_31_,
mastertena0_.deleted as deleted7_31_,
mastertena0_.value_id as value_id8_31_,
mastertena0_.param_id as param_id9_31_,
mastertena0_.tenant_id as tenant_id10_31_,
mastertena0_.value_name as value_name11_31_
from
v2_mst_tenant_param_value mastertena0_
where
mastertena0_.tenant_id=?
and mastertena0_.param_id=?
and (
lower(mastertena0_.value_name) like ?
)
group by
mastertena0_.value_id ,
mastertena0_.value_name ,
mastertena0_.param_id
having
count(mastertena0_.surrogate_key)>=1
order by
mastertena0_.value_name asc )
where
rownum <= ?