0

I have a problem with my custom query made with criteria query, I need to make an In clause on a field that has a many to many relationship, what is the best way?

here is the entity

public class Book {

    private int idBook;

    //bi-directional many-to-many association to Author
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
        name="book_author"
        , joinColumns={
            @JoinColumn(name="idBook")
            }
        , inverseJoinColumns={
            @JoinColumn(name="idAuthor")
            }
        )
    private Set<Author> authors;

here is my wrong attempt

        if (filterValue.getAuthors() != null) {
            In<Integer> authorsInId = cb.in(root.get("authors"));
            for (AuthorsPOJO subsystem : filterValue.getAuthors()) {
                authorsInId.value(authors.getIdAuthor());
            }
            predicates.add(authorsInId);
        }

thanks

  • Please add more details like Author class, controller, service, repository to find out the problem – Eklavya Apr 09 '20 at 13:53
  • Here you can find more detail, thanks https://stackoverflow.com/questions/60969599/spring-criteria-query-in-clause – AndreaB Apr 10 '20 at 07:07

0 Answers0