{
"type": "https://www.jhipster.tech/problem/problem-with-message",
"title": "Internal Server Error",
"status": 500,
"detail": "Specified result type [java.lang.Long] did not match Query selection type [com.segmentcreation.domain.SegmentRules] - multiple selections: use Tuple or array",
"instance": "/api/segment-rules/filter",
"message": "error.http.500",
"path": "/api/segment-rules/filter"
}
package com.segmentcreation.repository;
import com.segmentcreation.domain.SegmentRules;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
@Repository
public class SegmentRulesRepositoryCustomImpl implements SegmentRulesRepositoryCustom {
@PersistenceContext
private EntityManager entityManager;
private Set<String> getValidSegmentRules() {
Set<String> validColumns = new HashSet<>();
Field[] fields = SegmentRules.class.getDeclaredFields();
for (Field field : fields) {
validColumns.add(field.getName());
}
return validColumns;
}
@Override
public Page<SegmentRules> findByDynamicFilter(String segmentRule, String value, String logicOperator, Pageable pageable) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<SegmentRules> query = cb.createQuery(SegmentRules.class);
Root<SegmentRules> root = query.from(SegmentRules.class);
List<Predicate> predicates = new ArrayList<>();
Set<String> validSegmentRules = getValidSegmentRules();
// Validate segmentRule dynamically
if (segmentRule != null && value != null) {
if (!validSegmentRules.contains(segmentRule)) {
throw new IllegalArgumentException("Invalid segment rule: " + segmentRule);
}
// Use LIKE for partial matching
predicates.add(cb.equal(root.get(segmentRule), value));
}
// Use AND/OR logic
Predicate finalPredicate;
if ("OR".equalsIgnoreCase(logicOperator)) {
finalPredicate = cb.or(predicates.toArray(new Predicate[0]));
} else { // Default to AND
finalPredicate = cb.and(predicates.toArray(new Predicate[0]));
}
query.where(finalPredicate);
// Pagination
List<SegmentRules> resultList = entityManager.createQuery(query)
.setFirstResult((int) pageable.getOffset())
.setMaxResults(pageable.getPageSize())
.getResultList();
// Count query for pagination
CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
Root<SegmentRules> countRoot = countQuery.from(SegmentRules.class);
countQuery.select(cb.count(countRoot)).where(finalPredicate);
Long total = entityManager.createQuery(countQuery).getSingleResult();
return new PageImpl<>(resultList, pageable, total);
}
}