针对 PostgreSQL 数据库的 HQL 查询:
var checkLines = _Session.CreateQuery(
@"select lines from FinancialStatement statement
inner join fetch statement.FinancialStatementLines lines
where statement.FinancialStatementId = :statementId
and lines.TransactionType = :transactionType
and length(lines.CheckNumber) > 0")
.SetParameter("statementId", statement.FinancialStatementId)
.SetParameter("transactionType", TransactionTypes.Debit)
.List<FinancialStatementLine>();
查询对我来说看起来不错,但我是 HQL 的新手。有人可以告诉我我做错了什么吗?我假设问题出在 HQL 上,如果您认为我需要查看其他地方,请告诉我。
情节变浓
检查上述 HQL 创建的查询后,我发现它看起来像这样:
select
financials1_.LineId as LineId14_,
financials1_.FinancialStatementId as Financia2_14_,
financials1_.APPaymentID as APPaymen3_14_,
financials1_.EffectiveDate as Effectiv4_14_,
financials1_.Amount as Amount14_,
financials1_.TransactionType as Transact6_14_,
financials1_.CheckNumber as CheckNum7_14_,
financials1_.Description as Descript8_14_,
financials1_.VendorDescription as VendorDe9_14_,
financials1_.FinancialStatementId as Financia2_,
financials1_.LineId as LineId
from
FinancialStatements financials0_
where
financials0_.FinancialStatementId=:p0
and financials1_.TransactionType=:p1
and length(financials1_.CheckNumber)>0
...现在,WTF? select
子句别名在 from
子句中不存在,它解释了错误,但当然没有解释错误存在的原因。
我该如何解决这个问题?
最佳答案
事情是这样的:显然,当使用 HQL 时,要生成正确的查询,您需要将您尝试获取的对象类型作为查询的第一部分,例如:
var paymentLines = _Session.CreateQuery(
@"select lines from FinancialStatementLine lines
inner join fetch lines.Statement statement
where statement.FinancialStatementId = :statementId
and lines.TransactionType = :transactionType")
.SetParameter("statementId", statement.FinancialStatementId)
.SetParameter("transactionType", TransactionTypes.Debit)
.List<FinancialStatementLine>();
这行得通,而其他查询行不通。在 SQL 中,连接的顺序无关紧要;在 HQL 中,它似乎很重要。
关于hibernate - HQL 42P01 : missing FROM-clause entry for table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11130881/