Skip to content

Commit

Permalink
- Fixed HibernateDAO to return articles that contain AT LEAST ONE tag…
Browse files Browse the repository at this point in the history
… from the provided criteria, instead of requiring all of them to be present
  • Loading branch information
Eukon05 committed Oct 20, 2024
1 parent 88788ac commit 27e9876
Showing 1 changed file with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Persistence;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.*;
import ovh.eukon05.infodb.api.persistence.ArticleDAO;
import ovh.eukon05.infodb.api.persistence.ArticleDTO;
import ovh.eukon05.infodb.api.persistence.ArticleSearchCriteria;
Expand Down Expand Up @@ -72,13 +69,12 @@ public List<ArticleDTO> findByCriteria(ArticleSearchCriteria criteria, int pageN
if (Optional.ofNullable(criteria.dateTo()).isPresent()) {
predicates.add(cb.lessThanOrEqualTo(root.get("datePublished"), criteria.dateTo()));
}
// This predicate checks if ALL the tags belong to the article, not if at least one!
if (Optional.ofNullable(criteria.tags()).isPresent() && !criteria.tags().isEmpty()) {
for (String tag : criteria.tags()) {
predicates.add(cb.isMember(tag, root.get("tags")));
}
Join<ArticleEntity, String> join = root.join("tags");
predicates.add(join.in(criteria.tags()));
}


cr.where(cb.and(predicates.toArray(new Predicate[]{}))).orderBy(cb.desc(root.get("datePublished")));
return em.createQuery(cr)
.setFirstResult(pageNo * PAGE_SIZE)
Expand Down

0 comments on commit 27e9876

Please sign in to comment.