Мне удалось решить эту проблему с помощью ответа Абины sh. Ниже приведен рабочий код для повторно используемого класса спецификации
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.ec.application.model.Product;
import com.ec.common.Filters.FilterAttributeData;
import com.ec.common.Filters.FilterDataList;
public class SpecificationsBuilder<T>
{
//#######################################/#################//
// Level 0 - If the field that you want to query is parent entity //
//########################################################//
public Specification<T> whereDirectFieldContains(String key,List<String> names)
{
Specification<T> finalSpec = null;
for(String name:names)
{
Specification<T> internalSpec = (Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb)
-> cb.like(root.get(key), "%"+ name +"%");
finalSpec = specOrCondition(finalSpec,internalSpec); // to append specifications as I am filtering based on list of strings
}
return finalSpec;
}
//#######################################//
// Level 1 - If you want to query a child entity. // //
//#######################################//
public Specification<T> whereChildFieldContains(String childTable, String childFiledName,
List<String> names)
{
Specification<T> finalSpec = null;
for(String name:names)
{
Specification<T> internalSpec = (Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb)
-> cb.like(root.get(childTable).get(childFiledName), "%"+ name +"%");
finalSpec = specOrCondition(finalSpec,internalSpec); // to append specifications as I am filtering based on list of strings
}
return finalSpec;
}
//#######################################//
// Reusable Spec Setter to handle NULLs. //
//#######################################//
public Specification<T> specAndCondition(Specification<T> finalSpec, Specification<T> internalSpec)
{
if(finalSpec == null) return internalSpec;
else return finalSpec.and(internalSpec);
}
public Specification<T> specOrCondition(Specification<T> finalSpec, Specification<T> internalSpec)
{
if(finalSpec == null) return internalSpec;
else return finalSpec.or(internalSpec);
}
}
И это код для класса спецификации объекта
public static Specification<Product> getSpecification(FilterDataList filterDataList)
{
List<String> productNames = specbldr.fetchValueFromFilterList(filterDataList,"product");
List<String> categoryNames = specbldr.fetchValueFromFilterList(filterDataList,"category");
Specification<Product> finalSpec = null;
if(productNames != null && productNames.size()>0)
finalSpec = specbldr.specAndCondition(finalSpec, specbldr.whereDirectFieldContains(Product_.PRODUCT_NAME, productNames));
if(categoryNames != null && categoryNames.size()>0)
{
finalSpec = specbldr.specAndCondition(finalSpec,
specbldr.whereChildFieldContains(Product_.CATEGORY,Category_.CATEGORY_NAME, categoryNames));
}
return finalSpec;
}