gpt4 book ai didi

java - 连接错误 JPA 和named_queries xml 的预期路径

转载 作者:行者123 更新时间:2023-12-01 17:30:10 24 4
gpt4 key购买 nike

由于这个错误,我对 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]

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

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。

最佳答案

您在查询中使用了别名 (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

the Hibernate documentation 中详细描述了 HQL 和关联。 。阅读它。

关于java - 连接错误 JPA 和named_queries xml 的预期路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11835195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com