Skip to content

Commit

Permalink
Merge pull request #281 from jpos/fix/dbmanager-npe
Browse files Browse the repository at this point in the history
Refactor in createCriteriaQuery to avoid npe and duplicate results
  • Loading branch information
ar authored Sep 13, 2023
2 parents 674af76 + 60e29de commit a18c439
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class DBManager<T> {

Expand Down Expand Up @@ -265,9 +266,16 @@ protected final CriteriaQuery<T> createCriteriaQuery(boolean internalFilters, DB
CriteriaQuery<T> query = createCriteriaQuery(internalFilters, (DBFilter<T>) null);
CriteriaBuilder cb = db.session().getCriteriaBuilder();
Root<T> root = query.from(clazz);

return query.where(query.getRestriction()
, cb.and(Arrays.stream(filters).map(f -> f.createPredicate(cb, root)).toArray(Predicate[]::new)));
Predicate combinedPredicate = cb.and(Arrays.stream(filters)
.filter(f -> f != null)
.map(f -> f.createPredicate(cb, root))
.toArray(Predicate[]::new));
Predicate restriction = query.getRestriction();
if (restriction != null) {
combinedPredicate = cb.and(restriction,combinedPredicate);
}
query.distinct(true);
return query.where(combinedPredicate);
}

private CriteriaQuery<T> createQueryByParam(String param, Object value, boolean withFilter) {
Expand Down

0 comments on commit a18c439

Please # to comment.