13

我有 2 个实体

PayoutHeader.java

@Entity
public class PayoutHeader extends GenericDomain implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;        
    @Column
    private Integer month;
    @Column
    private Integer year;
    @OneToOne
    private Bank bank;
    @Column
    private Double tdsPercentage;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date **chequeIssuedDate**;

    @Temporal(javax.persistence.TemporalType.DATE)
    private Date entryDate;

}

支付明细.java

@Entity
public class PayoutDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;        

    @ManyToOne
    private PayoutHeader payoutHeader;

    @Column
    private Double amount;
    @Column
    private String bankName;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date clearingDate;
    @OneToOne    
    private Advisor advisor;

    @Column
    private Long **advisorId**;
}

我想使用 Hibernate Criteria 编写查询,例如

Select pd.* from PayoutDetails pd, PayoutHeader ph where pd.payoutheaderId = ph.id and pd.advisorId = 1 and and ph.chequeIssuedDate BETWEEN STR_TO_DATE('01-01-2011', '%d-%m-%Y') AND STR_TO_DATE('31-12-2011', '%d-%m-%Y') ";

我写过这样的查询

public List<PayoutDetails> getPayoutDetails(AdvisorReportForm advisorReportForm) {
        Criteria criteria = getSession().createCriteria(PayoutDetails.class);

        if (advisorReportForm.getAdvisorId() != null && advisorReportForm.getAdvisorId() > 0) {
            criteria.add(Restrictions.eq("advisorId", advisorReportForm.getAdvisorId().toString()));
        }

        criteria.setFetchMode("PayoutHeader", FetchMode.JOIN)
                .add(Restrictions.between("chequeIssuedDate", advisorReportForm.getFromDate(), advisorReportForm.getToDate()));        

        return criteria.list();
    }

但是给出错误为

org.hibernate.QueryException:无法解析属性:chequeIssuedDate of:org.commission.domain.payout.PayoutDetails

我认为是试图在 中查找chequeIssuedDate字段PayoutDetails,但该字段在PayoutHeader. 如何在加入期间指定别名?

4

1 回答 1

14

criteria.setFetchMode("PayoutHeader", FetchMode.JOIN)只是指定您要通过连接获取标头,在这种情况下可能不需要。它不会更改限制中使用的表。为此,您可能希望创建一个附加条件或别名,或多或少如下:

public List<PayoutDetails> getPayoutDetails(AdvisorReportForm advisorReportForm) {
        Criteria criteria = getSession().createCriteria(PayoutDetails.class);

        if (advisorReportForm.getAdvisorId() != null && advisorReportForm.getAdvisorId() > 0) {
            criteria.add(Restrictions.eq("advisorId", advisorReportForm.getAdvisorId().toString()));
        }

        criteria.createCriteria("payoutHeader")
                .add(Restrictions.between("chequeIssuedDate", advisorReportForm.getFromDate(), advisorReportForm.getToDate()));        

        return criteria.list();
    }

或(使用别名)

public List<PayoutDetails> getPayoutDetails(AdvisorReportForm advisorReportForm) {
        Criteria criteria = getSession().createCriteria(PayoutDetails.class);

        if (advisorReportForm.getAdvisorId() != null && advisorReportForm.getAdvisorId() > 0) {
            criteria.add(Restrictions.eq("advisorId", advisorReportForm.getAdvisorId().toString()));
        }

        criteria.createAlias("payoutHeader", "header")
                .add(Restrictions.between("header.chequeIssuedDate", advisorReportForm.getFromDate(), advisorReportForm.getToDate()));        

        return criteria.list();
    }

有关此示例,请参阅有关标准查询的 Hibernate 文档。

将 转换为字符串也可能不合适advisorId,因为它实际上是 aLong并且可能映射到 sql 中的数字字段。

advisorId如果您映射advisor, 并使用基于字段的限制,通常也根本不映射类似此字段的内容advisor,类似于处理payoutHeader字段的方式。

我不担心从标头中获取所有字段,但是如果您让createCriteria版本正常工作,它的行为可能会有所不同。

于 2012-07-16T11:13:58.027 回答