Hibernate 标准、连接或

发布于 2024-09-18 03:37:13 字数 1715 浏览 4 评论 0原文

我正在尝试使用连接表、使用 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: 无法解析属性:回顾案例学生信息:案例回顾。我还尝试创建多层别名:

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

并在restriction.or 中使用它,得到相同的结果。奇怪的是,任何一组别名都可以在

Order.asc( "reviewCaseStudentInformation.lastName")

几乎不知所措的情况下正常工作。建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2024-09-25 03:37:16

您需要创建另一个标准 - 即子标准 - 像这样:

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

PS。我认为 ReviewCaseCaseReview 上的一个字段并没有帮助......它使代码相当混乱!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文