2014-01-09 49 views
0

我有兩個表父和子。我想在Hibernate的標準使用查詢createAlias/createCriteria hibernate INNER_JOIN

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查詢,再次感謝您的幫助。

回答

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(); 

希望我很清楚地解釋。