开发者

Hibernate - filtering by associated property

开发者 https://www.devze.com 2022-12-24 15:10 出处:网络
I have an ExamResult class which has a link back to it\'s parent Exam. I wish to filter by a boolean property on exam result.

I have an ExamResult class which has a link back to it's parent Exam.

I wish to filter by a boolean property on exam result.

List<ExamResult> examResults = session
                    .createCriteria(ExamResult.class)
                    .createCriteria("exam") // 3.
                    .add( Restrictions.eq("primaryExam", Boolean.TRUE) ) // 4.
                    .list();

I can retrieve all rows correctly without lines 3 and 4 added. With these additional lines I get the following error:

org.hibernate.QueryException: not an association: exam

I'm unsure whether I'm going about this the right way. The hibernate tutorial is unclear to me.

T开发者_Go百科he relationship I used can be seen in this tutorial.

ExamResult has:

// bidirectional association! Needed to trick hibernate ;P
@Column(name="exam_id", nullable=false, updatable=false, insertable=false)
private Long exam;

Exam has

//----bidirectional association
@OneToMany(mappedBy="exam")
private final List<ExamResult> examResults = new ArrayList<ExamResult>();


The problem is that the in your ExamResult mapping should be:

@Column(name="exam_id", nullable=false)
private Exam exam;

Otherwise, your criteria looks fine assuming you have a boolean property primaryExam in Exam.

0

精彩评论

暂无评论...
验证码 换一张
取 消