我有一个 ExamResult
类,它有一个返回其父 Exam
的链接。
我希望按考试结果的 boolean 属性进行过滤。
List<ExamResult> examResults = session
.createCriteria(ExamResult.class)
.createCriteria("exam") // 3.
.add( Restrictions.eq("primaryExam", Boolean.TRUE) ) // 4.
.list();
我可以正确检索所有行,而无需添加第 3 行和第 4 行。通过这些附加行,我收到以下错误:
org.hibernate.QueryException: not an association: exam
我不确定我是否以正确的方式处理这件事。 hibernate tutorial我不清楚。
我使用的关系可以在this tutorial中看到.
考试结果有:
// bidirectional association! Needed to trick hibernate ;P
@Column(name="exam_id", nullable=false, updatable=false, insertable=false)
private Long exam;
考试有
//----bidirectional association
@OneToMany(mappedBy="exam")
private final List<ExamResult> examResults = new ArrayList<ExamResult>();
最佳答案
问题是您的 ExamResult
映射应该是:
@Column(name="exam_id", nullable=false)
private Exam exam;
否则,假设您在 Exam
中有一个 boolean 属性 primaryExam
,那么您的标准看起来不错。
关于java - Hibernate - 按关联属性过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2502600/