【发布时间】:2014-07-21 21:17:00
【问题描述】:
这个answer 清楚地描述了如何设置Restriction 来比较两个属性是否相等。
鉴于该答案中的这两个实体:
@Entity
public class Professor{
K id;
List<Student> students;
}
@Entity
public class Student{
K profid;
String name;
}
我将如何编写以下Restriction:where professor.students.name is "Kevin"?
我尝试了类似的方法:
Criteria criteria = createCriteria(Professor.class);
criteria.add(Restrictions.eq("students.name", "Kevin")
但失败了:
org.hibernate.QueryException: could not resolve property:
'students.name' of: Professor
【问题讨论】:
-
您需要先添加一个别名,例如
criteria.addAlias("students", "s");,然后您可以使用Restrinctions.eq("s.name" "Kevin");。需要别名,因为 Criteria API 不会自动加入 -
谢谢,那行得通。想发帖换取信用吗?