0

由于这个错误,我对 JPA 和 Hibernate 感到非常困惑。

这是堆栈异常

2012-08-07 03:55:04,277 ERROR [org.hibernate.hql.PARSER] (HDScanner)  Path expected for join!
2012-08-07 03:55:04,299 ERROR [org.hibernate.impl.SessionFactoryImpl] (HDScanner) Error in named query: getTotalCallReportsFromQuarter
org.hibernate.QueryException: Unable to resolve path [care.quarter], unexpected token [care] [SELECT count(noOfCallReports) FROM com.business.model.base.CareCallReport JOIN Relationships with CareCallReport.clientAccountNo=Relationships.clientAccountNo where UID = :UID and care.quarter = :quarter and care.year = :year]

这是我计划加入的两个实体:

CareCallReport.java
@Entity
@Table(name = "CARE_CALLREPORT")
public class ICareCallReport implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ClientAccountNo", nullable = false, length = 7)
    private String clientAccountNo;

    @Column(name = "NoOfCallReports", nullable = false, length = 11)
    private int noOfCallReports;

    @Column(name = "Quarter", nullable = false, length = 11)
    private int quarter;

    @Column(name = "QtrYear", nullable = false, length = 4)
    private int year;

    ... Getters and Setters

和另一个实体

ITRelationships.java
@Entity
@Table(name = "IT_RELATIONSHIPS")
public class ITRelationships implements Serializable {

    @Id
    @Column(name = "ClientID",nullable = false, length = 7)
    private String clientID;

    @Column(name = "ClientAccountNo", nullable = false, length = 7)
    private String clientAccountNo;

    @Id
    @Column(name = "UID", nullable = false, length = 6)
    private String UID;

    @Column(name = "Quarter", nullable = false, length = 11)
    private int quarter;

    @Column(name = "QtrYear", nullable = false, length = 4)
    private int year;

    Getters and Setters

我写的 JQL 方法是这样的:

<access>FIELD</access>
<named-query name="getTotalCallReportsFromQuarter">
    <query>
        <![CDATA[
        SELECT count(noOfCallReports)
        FROM CareCallReport JOIN ITRelationships
        with CareCallReport.clientAccountNo = ITRelationships.clientAccountNo
        where UID = :uid
            and care.quarter = :quarter
            and care.year = :year
        ]]> 
    </query>
</named-query>

我做错了什么?这是我第一次接触 JPA。

4

2 回答 2

3

您在查询 ( icare) 中使用别名但未定义它。此外,只有在实体之间存在关联(OneToOne、OneToMany、ManyToOne 或 ManyToMany)时,才能进行连接。假设你没有,查询应该是

SELECT count(care.noOfCallReports)
    FROM CareCallReport care, ITRelationships relationship
    where relationship.UID = :uid
        and care.quarter = :quarter
        and care.year = :year
        and care.clientAccountNo = relationship.clientAccountNo

HQL 和关联在Hibernate 文档中有很好的描述。阅读。

于 2012-08-06T20:27:50.267 回答
2

您似乎忘记在查询中使用别名。

但我还是想再解释一下。使用 JPA/Hibernate,您不仅可以将 DB 中的表映射到 POJO(实体),还可以映射关系。

如果我理解正确的话,从 IT_RELATIONSHIPS 到 CARE_CALLREPORT 是一对一的关系。在面向对象的术语中,ITRelationships 引用了 ICareCallReport。

此外,ClientAccountNo 实际上不是 IT_RELATIONSHIPS 实体的属性,它只是对 CARE_CALLREPORT 实体的引用。

ITRelationships.java
@Entity
@Table(name = "IT_RELATIONSHIPS")
public class ITRelationships implements Serializable {

    @Id
    @Column(name = "ClientID",nullable = false, length = 7)
    private String clientID;

    @OneToOne
    @JoinColumn(name="ClientAccountNo")
    private ICareCallReport careCallReport;

    ....

现在查询变得简单多了。记住这不是 SQL,这是对象查询。

    <named-query name="getTotalCallReportsFromQuarter">
    <query>
        <![CDATA[
        SELECT itr.careCallReport.noOfCallReports
        FROM ITRelationships itr
        where itr.UID = :uid
            and itr.careCallReport.quarter = :quarter
            and itr.careCallReport.year = :year
        ]]> 
    </query>
    </named-query>
于 2012-08-06T20:49:10.720 回答