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.