Write a criteria query on elements of a Set

543 views Asked by At

how I can write a criteria query if a parameter is a Set? In the class Libro autore is a Set, in my criteria query I want to check on author first name and author last name. If I wirte this method surely don't work because autores is a Set and authorFirstName is a String.

public static Specification<Libro> filterByAuthorFirstName(final String authorFirstName){
    return new Specification<Libro>() {
        @Override
        public Predicate toPredicate(Root<Libro> root, CriteriaQuery<?> query, CriteriaBuilder cb){
            return cb.equal((root.get(Libro_.autores)), authorFirstName);
        }
    };
}

The class Libro_ generated is

    package com.dennis.casalibro.domain;

import javax.annotation.Generated;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Libro.class)
public abstract class Libro_ {

    public static volatile SingularAttribute<Libro, String> descrizione;
    public static volatile SingularAttribute<Libro, String> titolo;
    public static volatile SingularAttribute<Libro, Integer> numero_pagine;
    public static volatile SingularAttribute<Libro, CasaEditrice> casaEditrice;
    public static volatile SingularAttribute<Libro, Integer> anno_pubblicazione;
    public static volatile SingularAttribute<Libro, Integer> rating;
    public static volatile SingularAttribute<Libro, String> copertina_url;
    public static volatile SingularAttribute<Libro, Long> id;
    public static volatile SetAttribute<Libro, Autore> autores;
    public static volatile SingularAttribute<Libro, Genere> genere;

}
0

There are 0 answers