我在编写条件查询时遇到以下问题我正在使用Hibernate JPA 2.0,我正在尝试创建一个条件查询来获得Debts
DebtHolder是一个Join表债务(1)-->(N)债务持有人(N)-->(1)一方
Party有两个子类Person和Org
人员具有属性lastName
当我在lastName上尝试where子句时,我得到一个错误,说"java.lang.IllegalArgumentException:Unable to resolve attribute[lastName]against path[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;
}
请让我知道我错过了什么。。。谢谢
在参与方、人员和组织之间有继承层次结构。只有人员的属性名称为"lastName"。在您的JPA实体中,您指的是私营方债务人;这意味着债务人是一方,其中没有姓氏。尝试使用私人债务人;而非私营方债务人;