views:

60

answers:

1

I'm trying to accomplish ad-hoc queries using joined tables, using an Restrictions.or and Restrictions.ilike

My entities look like:

@Entity
@Table(name="CASE_REVIEW")
public class CaseReview {

    @Id
    @Column(name = "REVIEW_ID", unique = true, nullable = false, length = 19)
    private Long reviewId;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CASE_ID", nullable = false)
    private Case reviewCase;

 }

 @Entity
 @Table(name = "CASE")
 public class Case {

    @Id
    @Column(name = "ID", unique = true, nullable = false, length = 19)
    private Long id;

    @OneToOne( fetch=FetchType.EAGER )
    @JoinColumn( name="STUDENT_ID" , referencedColumnName="ID", 
    private StudentInformation studentInformation;

 }

 @Entity
 @Table( name="STUDENT")
 public class StudentInformation {
    @Id
    @Column( name="ID")
    private Long id;

    @Column( name="LAST_NAME")
    private String lastName;

    @Column( name="FIRST_NAME")
    private String firstName;
 }

My code does something like the following:

Criteria c = session.createCriteria( CaseReview.class );
c.createAlias( "reviewCase" , "reviewCase"); 
c.createAlias( "reviewCase.studentInformation" , "reviewCasestudentInformation");
c.add( Restrictions.or( Restrictions.ilike("reviewCasestudentInformation.lastName" , "%e%" ), Restrictions.ilike( "reviewCasestudentInformation.firstName" , "%e" )));

I'm getting org.hibernate.QueryException: could not resolve property: reviewCasestudentInformation of: CaseReview. I've also tried creating multi-tier aliases:

c.createAlias( "reviewCase.studentInformation" , "reviewCaseStudentInformation");

and using that in the restriction.or with the same results. Strangely enough either set of aliases work fine for

Order.asc( "reviewCaseStudentInformation.lastName")

Pretty much at a loss. Suggestions?

A: 

You need to create another criteria — ie a subcriteria — like this:

Criteria criteraCaseReview = session.createCriteria(CaseReview.class);
Criteria criteraReviewCase = criteraCaseReview.createCriteria("reviewCase");
criteraReviewCase.add(Restrictions.or(Restrictions.ilike("lastName", "%e%"), Restrictions.ilike( "firstName", "%e")));

PS. I reckon it doesn't help that ReviewCase is a field on CaseReview... it makes the code rather confusing!

gutch