我有一个班级AppUser
;
class AppUser {
private String firstName;
private String lastName;
//-- getters and setters
}
我还有另一个班级Student
;
class Student {
private AppUser appUser;
private Date dateOfBirth;
//-- getters and setters
}
我如何搜索学生John Doe
,firstName John,lastName Doe?
如果是出生日期属性,我会在日期创建Criteria
并添加相等限制(Restristions.eq
)。我如何为AppUser对象中的lastName和firstName执行此操作?
答案 0 :(得分:11)
您可能需要添加别名...类似于:
List students = session.createCriteria(Student.class).createAlias("appUser", "user").add(Restrictions.eq("user.firstName", firstName)).list();
没有别名:
List students = session.createCriteria(Student.class).add(Restrictions.eq("appUser.firstName", firstName)).list();
答案 1 :(得分:8)
查询:
Query q = session.createQuery(
"SELECT s from Student s WHERE s.appUser.firstName=:firstName AND s.appUser.lastName=:lastName");
q.setParameter("firstName", "John");
q.setParameter("lastName", "Doe");
要使用标准,请检查this thread