tags:

views:

106

answers:

1

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.

The 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>();
A: 

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.

Manuel Darveau
Thanks - this now shows `org.hibernate.MappingException: Repeated column in mapping for entity: Exam column: exam_id (should be mapped with insert="false" update="false")` - I presume the repeated column is to do with the hibernate trick in the tutorial.
Pool
Can you paste the complete ExamResult? Try removing the name="exam_id" part. If you are not mapping to an already existing database, it's useles. BTW, if an answer is usefull, dont forget to upvote or accept it :-).
Manuel Darveau