createAlias / createCriteria hibernate INNER_JOIN

时间:2014-01-09 15:59:26

标签: java oracle hibernate

我有两个表Parent和Child。我想在Hibernate Criteria中使用的查询

SELECT tcr.*
  FROM case_reminders tcr
 INNER JOIN case_reminder_opr tco ON tcr.case_id = tco.case_id
 WHERE tcr.case_status = 'OPN'
   AND tco.operator_id = 111;

我已将标准写为

    Criteria ctr = getSession().createCriteria(CaseReminderOpr.class).add(Restrictions.eq("pk.oprOperatorId", operatorId));
    ctr.createCriteria("pk.crmCaseId", "CR", Criteria.INNER_JOIN).add(Restrictions.eq("CR.caseStatus", STATUS.OPEN.getValue()));
    List<CaseReminderOpr> oprList = ctr.list();

尝试使用createAlias,但我收到错误

ORA-00904:“CR1 _”。“CASE_STATUS”:标识符无效

CaseReminders(Parent)和CaseReminderOpr(Child)的类如下。

@Entity
@Table(name = "CASE_REMINDERS")
public class CaseReminders implements Serializable {
    @Id
    @Column(name = "CASE_ID")
    private Long caseId;

    @Column(name = "CASE_STATUS")
    private String caseStatus;
}


@Entity
@Table(name="CASE_REMINDER_OPR")
public class CaseReminderOpr implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private CaseReminderOprPK pk;
}

@Embeddable
public class CaseReminderOprPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @ManyToOne
    @JoinColumn(name = "CASE_ID")
    private CaseReminders crmCaseId;

    @Column(name="OPERATOR_ID")
    private Long operatorId;
}

请帮我查看inner_join查询,再次感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

改变将如下,然后它起作用。我后来才意识到这一点。

在主要实体类中将Joincolumn设为 insertable = false,updatable = false

   @Entity
    @Table(name="CASE_REMINDER_OPR")
    public class CaseReminderOpr implements Serializable {
        private static final long serialVersionUID = 1L;

        @EmbeddedId
        private CaseReminderOprPK pk;

        @ManyToOne
        @JoinColumn(name = "CASE_ID", insertable=false, updatable=false)
        private CaseReminders caseRem;
    }

现在查询应该按预期工作。

Criteria ctr = getSession().createCriteria(CaseReminderOpr.class, "CRO").add(Restrictions.eq("pk.oprOperatorId", operatorId));
    ctr.createCriteria("CRO.caseRem", "CR", Criteria.INNER_JOIN).add(Restrictions.eq("CR.caseStatus", STATUS.OPEN.getValue()));
    List<CaseReminderOpr> oprList = ctr.list();

希望我能解释清楚。