我有以下类定义了两个实体之间的关系,并且我建立了一个似乎适用于基本用例的搜索条件:实体如下(为简洁起见,省略了一些信息!):>
public class Category implements Serializable {
@Id
private Long id;
private String name;
@ManyToOne
private Category parent;
}
//getters ands setters
public class Product implments Serializable {
@Id
private Long id;
private String name;
..
@ManyToOne
private Category category;
}
//getters and setters
我对类别名称的搜索说明如下:
public class ProductSearchSpecification extends BaseSpecification<Product, ProductSearchtRequest> {
@Override
public Specification<Product> getFilter(ProductSearchRequest request) {
return (root, query, cb) -> {
query.distinct(true);
return where(categoryNameContains(request.categoryName))
.toPredicate(root, query, cb);
};
}
private Specification<Product> categoryNameContains(String categoryName) {
return categoryAttributeContains("name", categoryName);
}
private Specification<Product> categoryAttributeContains(String attribute, String value) {
return (root, query, cb) -> {
if (value == null) {
return null;
}
Join<Product, Category> category = root.join("category");
return cb.like(
cb.lower(category.get(attribute)),
containsLowerCase(value)
);
};
}
This works as expected. I however want to recursively search for all the child categories and get the products as well, ie if I have the following data
{id: 1,name:"Fashion"},
{id: 20, "name":"Footwear","parentId":1},
{id: 25, "name":"Sandals","parentId":20}
和一些类别25(凉鞋)的产品,当我使用在属性名称上搜索“鞋类”时,我希望能够使用这些产品。