【发布时间】:2016-02-17 07:08:53
【问题描述】:
假设我有两个实体:
@Entity
public class A {
@Id
private int id;
@ManyToOne
private B b;
//more attributes
}
@Entity
public class B {
@Id
private int id;
}
因此,A 的表有一个 b_id 列作为外键。
现在,我想根据其他字段的某些条件仅选择 b_id。如何使用条件查询来做到这一点?
我尝试做以下抛出 IllegalArgumentException 说"Unable to locate Attribute with the given name [b_id] on this ManagedType [A]"
CriteriaQuery<Integer> criteriaQuery = criteriaBuilder.createQuery(Integer.class);
Root<A> root = criteriaQuery.from(A.class);
Path<Integer> bId = root.get("b_id");
//building the criteria
criteriaQuery.select(bId);
【问题讨论】:
标签: java jpa criteria-api