Search code examples
jpaspring-data-jpahibernate-criteria

JPA Criteria Builder build a predicate on a setAttribute


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?


Solution

  • 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));
    }