0

I am new to using Criteria Builder for building dynamic queries.

I am trying to create a specification for my repository to find the DVD titles that contain a description like the search term.

I have a many to many join between dvds and dvd desciptions in my model.

In my DVD static metamodel class I have a

 public static volatile SetAttribute<DVD, DVDDescription> descriptions;

At the moment I am getting an error: "CriteriaBuilder is not applicable for the arguments"

 public Predicate toPredicate(Root<DVDs> root, CriteriaQuery<?>, CriteriaBuilder cb){

     return cb.like(searchTerm,root.get(DVD_.descriptions))

}

I know I am probably going about this the wrong way, but how do I use criteria builder for a setAttribute?

Sarah92
  • 671
  • 4
  • 12
  • 29

1 Answers1

2

You need a Join:

public Predicate toPredicate(Root<DVDs> root, CriteriaQuery<?>, CriteriaBuilder cb){
    Join<DVD, DVDDescription> descriptionJoin = root.join(DVD_.descriptions);
    return cb.like(searchTerm,descriptionJoin.get(DVDDescription_.content));
}
Julien E.
  • 46
  • 5
  • Many thanks! I had an entity class Profile : @Entity class Profile {@ManyToMany(mappedBy = "profiles")private Set groups = new HashSet<>(); } ; Using your method, i was able to leverage the CriteriaBuilder.In to filter Profiles by a given Groupe.id : ==> Join groupeJoin = root.join(Profile_.groups); predicates.add(criteriaBuilder.in(groupeJoin.get(Groupe_.ID)).value(criteria.getValue())); – manfall19 Jul 06 '21 at 19:58