JPA Criteria API where subclass - 出现错误:无法针对路径 [null] 解析属性 [lastName]
Posted
技术标签:
【中文标题】JPA Criteria API where subclass - 出现错误:无法针对路径 [null] 解析属性 [lastName]【英文标题】:JPA Criteria API where subclass - Getting error : Unable to resolve attribute [lastName] against path [null] 【发布时间】:2013-08-12 01:01:34 【问题描述】:我在编写条件查询时遇到以下问题 我正在使用 Hibernate JPA 2.0,我正在尝试创建一个条件查询来获取债务
DebtHolder 是一个连接表 债务(1)-->(N)DebtHolders(N)-->(1)当事人
Party 有两个子类 Person 和 Org
Person 有属性 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;
请让我知道我缺少什么...谢谢。
【问题讨论】:
您在这方面取得了进展吗?我相信我遇到了同样的问题。 【参考方案1】:您在 Party、Person 和 Org 之间具有继承层次结构。并且只有 Person 具有属性名称“lastName”。 在您的 JPA 实体中,您指的是 私人债务人; 这意味着债务人是 Party 并且没有姓氏。 尝试使用 私人债务人;而不是私人债务人;
【讨论】:
以上是关于JPA Criteria API where subclass - 出现错误:无法针对路径 [null] 解析属性 [lastName]的主要内容,如果未能解决你的问题,请参考以下文章
JPA/Criteria API - Like & equal 问题