I have a entity for product:
package com.javaschool.entity;
import lombok.*;
import javax.persistence.*;
import java.util.Set;
@EqualsAndHashCode(of = {"id"})
@ToString(of = { "id", "quantity", "price", "model"})
@Entity
@Table(name = "products")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "quantity")
private int quantity;
@Column(name = "price")
private int price;
@Column(name = "model")
private String model;
@Column(name = "is_active")
private boolean active;
@Column(name = "picture_url")
private String url;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "category_id")
private Category category;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "brand_id")
private Brand brand;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "season_id")
private Season season;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "color_id")
private Color color;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "material_id")
private Material material;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "size_id")
private Size size;
@ManyToMany(mappedBy = "productSet", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Set<Order> orderSet;
}
I want to filter by category, season, color, brand and other related parameters
At the moment my filtering function looks like this. It works for parameters such as model, price, quantity. That is, for those that are data in this table and not from others. How can I filter by parameters that are taken from other tables?
@Override
public List<Product> findByParam(List<SearchCriteria> params) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class);
Root<Product> root = criteriaQuery.from(Product.class);
Predicate predicate = criteriaBuilder.conjunction();
ProductSearchQueryCriteriaConsumer productConsumer = new ProductSearchQueryCriteriaConsumer(predicate, criteriaBuilder, root);
params.stream().forEach(productConsumer);
predicate = productConsumer.getPredicate();
criteriaQuery.where(criteriaBuilder.equal(root.get(Product_.active), true),
predicate);
List<Product> result = entityManager.createQuery(criteriaQuery).getResultList();
return result;
}
I thought that you can make such a call and everything will work. But I was wrong.
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
params.add(new SearchCriteria("season_id", ":", "3"));
List<ProductDto> productDtoList = productService.getProductsByParam(params);
My SearchCriteria
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SearchCriteria {
private String key;
private String operation;
private Object value;
}
Need to make this:
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
params.add(new SearchCriteria("category", ":", categoryRepository.findById(1)));
That is, in the searchcriteria for the value object, pass an object of this class to filter by