想使用spring jpa Specification的multiselect来选择某些列,但总是获取所有列,因此仅group by不起作用。
仅需要在query.multiselect中指定的列应返回否则我的分组依据将会中断。但是在下面的输出部分中看到它正在选择所有列。
.unescape
SQL的输出-
休眠:
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);