hibernate - HQL 42P01 : missing FROM-clause entry for table

标签 hibernate nhibernate postgresql hql npgsql

针对 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/

相关文章:

java - Spring Controller /服务/存储库中的泛型

nhibernate - 如何让 NHibernate 聚合函数 sum() 返回小数?

wpf - 在 MVVM WPF 中使用工作单元设计模式/NHibernate session

asp.net - 有人知道使用 ASP.NET MVC1 或 MVC2、NHibernate、Fluent NHibernate 和 CaSTLe 的可靠 Web 示例吗?

java - hibernate 性能问题,坚持一个接一个还是批量?

java - JPA或Hibernate生成一个(非主键)列值,不是从1开始

java - 外键为空 : Hibernate Spring

arrays - 如何在 PostgreSQL 8.4 中从 string_to_array() 返回一个元素?

json - PostgreSQL 函数 : Return Multiple Rows as JSON

sql - 我如何从 self 引用表中找到 parent 和 child 。?