JPA Criteria API where子类 - 获取错误:无法解析路径[null]的属性[lastName]

时间:2013-08-12 01:01:35

标签: hibernate jpa-2.0 criteriaquery

编写条件查询时,我遇到以下问题 我正在使用Hibernate JPA 2.0,我正在尝试创建一个条件查询来获取债务

DebtHolder是一个Join表 债务(1) - >(N)债权人(N) - >(1)甲方

Party有两个子类Person和Org

Person具有属性lastName

当我在lastName上尝试where子句时,我收到的错误是“java.lang.IllegalArgumentException:无法解析属性[lastName]对路径[null]”

以下是进行查询的方法:

  public List<Debt> searchByDebtNumber(String debtNumber, String taxId, String lastName, String phoneNumber, String address, String zip, String state) {

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Debt> c = cb.createQuery(Debt.class);
    Root<Debt> debt = c.from(Debt.class);
    Join debtHolders = (Join)debt.fetch("debtHolders", JoinType.LEFT);
    Join debtor = (Join)debtHolders.fetch("debtor", JoinType.LEFT);
    c.select(debt);
    c.distinct(true);


    List<Predicate> criteria = new ArrayList<Predicate>();
    if (debtNumber != null) {
        ParameterExpression<String> p = cb.parameter(String.class, "debtNumber");
        criteria.add(cb.equal(debt.get("debtNumber"), p));
        }

    if (taxId != null) {
        ParameterExpression<String> p = cb.parameter(String.class, "taxId");
        criteria.add(cb.equal(debtor.get("taxId"), p));
        }

    if (lastName != null) {

        ParameterExpression<String> p = cb.parameter(String.class, "lastName");
        criteria.add(cb.equal(debtor.get("lastName"), p));
        }


    if (criteria.size() == 0) {
           throw new RuntimeException("no criteria");
        } else if (criteria.size() == 1) {
           c.where(criteria.get(0));
        } else {
           c.where(cb.and(criteria.toArray(new Predicate[0])));
        }


    TypedQuery<Debt> q = entityManager.createQuery(c);
    if (debtNumber != null) { 
        q.setParameter("debtNumber", debtNumber);
        }

    if (taxId != null) { 
        q.setParameter("taxId", taxId);
        }
    if (lastName != null) { 
        q.setParameter("lastName", lastName);
        }

    return  q.getResultList();
}

以下是JPA实体的代码

            @Entity
            @Table(name = "debt")
            public class Debt {

                @NotNull
                @Column(name = "DEBT_NUMBER", nullable = false, unique = true)
                private String debtNumber;

                @OneToMany(mappedBy = "debt", fetch = FetchType.LAZY)
                private List<DebtHolder> debtHolders = new ArrayList<>();

            }

            @Entity
            @Table(name = "debt_holder")
            public class DebtHolder {

                @ManyToOne
                @NotNull
                @Property
                @JoinColumn(name = "DEBT_ID")
                private Debt debt;

                @ManyToOne(fetch = FetchType.LAZY)
                @NotNull
                @Property
                @JoinColumn(name = "DEBTOR_ID")
                private Party debtor;

            }

            @Entity
            @Inheritance(strategy = InheritanceType.JOINED)
            @Table(name = "PARTY")
            public class Party {

                @Basic
                @Column(name = "TAX_ID", length = 50)
                private String taxId;

            }

            @Entity
            @Table(name = "PERSON")
            public class Person extends Party {

                @Basic
                @NotNull
                @Column(name = "LAST_NAME")
                private String lastName;

            }

请让我知道我错过了什么......谢谢。

1 个答案:

答案 0 :(得分:0)

Party,Person和Org之间有继承层次结构。并且只有Person具有属性名称'lastName'。 在您的JPA实体中,您指的是 私人债务人; 这意味着债务人是Party并且没有lastName。 尝试使用 私人债务人;而不是私人党的债务人;