0

I've assembled the following class Specification:

 public ItemVendedorSpecification(String descricao, List<Long> categorias, List<Long> fabricantes, List<Long> vendedores) {
        super();
        this.descricao = descricao;
        this.categorias = categorias;
        this.fabricantes = fabricantes;
        this.vendedores = vendedores;
    }

    @Override
    public Predicate toPredicate(Root<ItemVendedor> root, CriteriaQuery<?> query, CriteriaBuilder builder) {

        if (!descricao.isEmpty()) {
            String PalavraChave[] = descricao.split(" ");
            for (String filtro : PalavraChave) {
                predicates.add(builder.like(builder.upper(root.get("id").get("produto").get("descricaoDetalhada")), "%" + filtro.toUpperCase() + "%"));
            }
        }

        predicates.add(builder.isTrue(root.get("disponivel")));

        if(!fabricantes.isEmpty()) {
            predicates.add(root.get("id").get("produto").get("fabricante").get("id").in(fabricantes));
        }

        if(!vendedores.isEmpty()) {
            predicates.add(root.get("id").get("vendedor").get("id").in(vendedores));

        }

        if(!categorias.isEmpty()) {
            predicates.add(root.join("id").get("produto").get("categorias").get("id").in(categorias));
        }

        return builder.and(predicates.toArray(new Predicate[1]));
    }
}

Almost all of the predicates are working, except the one that inserts the category criteria. It is not working and I'm having a hard time creating it.

In the way that it is returning the following error:

"Illegal attempt to dereference path source [null.produto.categorias] of basic type; nested exception is java.lang.IllegalStateException: Illegal attempt to dereference path source [null.produto.categorias] of basic type"

Could anyone help me in crafting this if?

Below is the detail of the ItemSeller class:

public class ItemVendedor implements Serializable{

    private static final long serialVersionUID = 1L;


    private ItemVendedorPK id = new ItemVendedorPK();
    private BigDecimal preco;
    private Boolean disponivel;
    private Date dt_insert;
    private Date dt_update;
    private BigDecimal desconto;

    public ItemVendedor() {

    }

    public ItemVendedor(Produto produto, Vendedor vendedor, BigDecimal preco, BigDecimal desconto ,Boolean disponivel) {
        super();
        this.id.setProduto(produto);
        this.id.setVendedor(vendedor);
        this.preco = preco;
        this.disponivel = disponivel;
        this.desconto = desconto;
    }

//GETs and SETs

As you can see it has a field called id that is a key composed of the Vendedor vendedor and Produto produto.

Inside the Produto class I have a List Categorias. For a product can belong to several categories.

In turn the Class category has the id.

What I want to put in the Specification, is a way to fetch all the ItemVendedor that have within their list of categorias some category that I cited as a parameter in another list List Categorias.

Gonzaga Neto
  • 246
  • 2
  • 3

1 Answers1

0

I got it sorted out. I was using the query in the wrong way. Below is the code for resolution:

  predicates.add (root.join ("id") join ("product") join ("categories") get ("id") in (categories));

So that we could carry out the verification to know if the product had some category that was within the parameter was necessary to realize some joins.

The first Join is in relation to what in the class code this is as id, this field in the ItemSeller table is actually a class for composite key (@ Embeddable), in turn this class had to perform the join with the class of Products, which had the Categories field.

So we can get to the list of categories of each product and thus buy if we have any element of this list within the informed query parameter.

Gonzaga Neto
  • 246
  • 2
  • 3