Closed spring-projects-issues closed 5 years ago
Oliver Drotbohm commented
This is correct I think. We need to execute two queries: one to retrieve the results and an additional count query to find out how many pages there are in total. Thus, the Specification
instance has to be applied to both of them, hence ….toPredicate(…)
is called twice.
What is the actual problem you are seeing?
grf110 commented
Look my java code
public class CriteriaSearch {
public List<SearchEntity> searchEntitys = new ArrayList<SearchEntity>();
private List<Predicate> predicates = new ArrayList<Predicate>();;
public <T> Specification<T> predicateBuild() {
return new Specification<T>() {
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
for (SearchEntity se : searchEntitys) {
String[] names = StringUtils.split(se.getFieldName(), ".");
Path expression = root.get(names[0]);
for (int i = 1; i < names.length; i++)
{ expression = expression.get(names[i]); }
predicates.add((Predicate) ReflectionUtils.invokeMethod(se.getOperator().getMethod(), builder, expression, se.getValue().toString()));
}
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
}
}
predicates value will be double.
I may be set predicates=null
resolve this problem
Oliver Drotbohm commented
Specifications need to be side effect free, which in your case they aren't
Jens Schauder commented
Batch closing resolved issue without a fix version and a resolution indicating that there is nothing to release (Won't fix, Invalid ...)
Specification is returning two sets of results with diferent criteria
Hi! My 5c to that too: My specification get executed twice too, so I get
Already registered a copy: SqmEntityValuedSimplePath ...
For some reason, it's a problem only on a specific class and I can't get why(To be specific, it's a problem when IN has more values inside )
grf110 opened DATAJPA-404 and commented
Use
findAll(Specification<T> spec, Pageable pageable)
. Problem: will be call methodSpecification.toPredicate(…)
twice.Reason: method
findAll(spec, pageable)
have callorg.springframework.data.jpa.repository.support.SimpleJpaRepository#readPage
andorg.springframework.data.jpa.repository.support.SimpleJpaRepository#getQuery
they all call
org.springframework.data.jpa.repository.support.SimpleJpaRepository#applySpecificationToCriteria
soPredicate predicate = spec.toPredicate(root, query, builder);
Run twice.
Affects: 1.4.1