简体   繁体   English

Hibernate使用标准和限制加入

[英]Hibernate Join using criteria and restrictions

I have 2 entities as 我有2个实体

PayoutHeader.java 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;

}

PayoutDetails .java PayoutDetails .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**;
}

I want to write query using Hibernate Criteria like 我想用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') ";

I have written query like this 我写了这样的查询

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

But is giving error as 但是给出了错误

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

I think is is trying to find chequeIssuedDate field in PayoutDetails , but this field is in PayoutHeader . 我想是试图在PayoutDetails找到chequeIssuedDate字段,但是这个字段在PayoutHeader How to specify alias during join ? 如何在加入期间指定别名?

The criteria.setFetchMode("PayoutHeader", FetchMode.JOIN) just specifies that you want to get the header by a join, and in this case is probably unneeded. criteria.setFetchMode("PayoutHeader", FetchMode.JOIN)只是指定您希望通过连接获取标头,在这种情况下可能不需要。 It doesn't change which table is used in the restrictions. 它不会更改限制中使用的表。 For that, you probably want to create an additional criteria or an alias, more or less as follows: 为此,您可能希望创建一个附加条件或别名,或多或少,如下所示:

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

or (using an alias) 或(使用别名)

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

See the Hibernate docs on Criteria Queries for examples of this. 有关示例,请参阅Criteria Queries上的Hibernate文档

It's also likely not appropriate to convert the advisorId to a string, as it is in fact a Long and probably mapped to a number field in sql. advisorId转换为字符串也可能不合适,因为它实际上是Long并且可能映射到sql中的数字字段。

It's common to also not map something like this advisorId field at all if you map the advisor , and use a restriction based on the advisor field, similarly to the way this deals with the payoutHeader field. 如果您映射advisor ,并且根据advisor字段使用限制,类似于处理payoutHeader字段的方式,通常也不会映射类似于此advisorId字段的payoutHeader

I wouldn't worry about getting all the fields from the header, but it may behave a bit differently if you get the createCriteria version to work. 我不担心从头文件中获取所有字段,但如果让createCriteria版本工作,它可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM