JPA规范不希望的基类

时间:2019-07-19 07:02:05

标签: java spring jpa specifications

我正在尝试实现一种方法来完成任务“将给定的文本与所有字符串类型的列进行比较”。

输入是将要比较其属性的类的列表。

public Specification<T> getByAllColumns(List<Class> list, String filterText) {
    List<Specification<T>> specificationList = new ArrayList<>();
    Specification<T> sep;

    for (Class class_ : list) {
        for (Field field : class_.getDeclaredFields())
            if (field.getType() == String.class) {
                sep = (Specification<T>) (root, criteriaQuery, criteriaBuilder) -> {
                    Predicate predicate;
                    From join = root;
                    System.err.println(root.getModel().getName());
                    if (root.getModel().getName().equalsIgnoreCase(class_.getName()))
                        join = fromJoin(join, class_.getName());
                    predicate = criteriaBuilder.equal(join.get(field.getName()), filterText);
                    return predicate;
                };
                specificationList.add(sep);
            }
    }

    Specification<T> specification = specificationList.get(0);
    for (int i = 1; i < specificationList.size(); i++)
        specification = or(specification, specificationList.get(i));

    return specification;
}

模型类

public class A extends Base {
    private String specialCode;
}

public class Base {
    private String id;
}

上面的代码为我提供了以下内容;

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [specialCode] on this ManagedType [com.a.model.Base]

0 个答案:

没有答案