我正在尝试使用连接表,使用Restrictions.or和Restrictions.ilike来完成即席查询
我的实体看起来像:
@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;
}
我的代码执行如下操作:
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" )));
我收到org.hibernate.QueryException:无法解析属性:reviewCasestudentInformation:CaseReview。我也尝试过创建多层别名:
c.createAlias( "reviewCase.studentInformation" , "reviewCaseStudentInformation");
并在restriction.or中使用它,结果相同。奇怪的是,任何一组别名都适用于
Order.asc( "reviewCaseStudentInformation.lastName")
几乎不知所措。建议?
答案 0 :(得分:0)
您需要创建另一个标准 - 即子标准 - 如下所示:
Criteria criteraCaseReview = session.createCriteria(CaseReview.class);
Criteria criteraReviewCase = criteraCaseReview.createCriteria("reviewCase");
criteraReviewCase.add(Restrictions.or(Restrictions.ilike("lastName", "%e%"), Restrictions.ilike( "firstName", "%e")));
PS。我认为ReviewCase
是CaseReview
上的一个字段并没有帮助...它会使代码相当混乱!