HQL query against a PostgreSQL database:
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>();
The query looks OK to me, but I am a newbie at HQL. Can someone tell me what I’m doing wrong? I’m assuming the problem is with the HQL, please let me know if you think I need to look elsewhere.
THE PLOT THICKENS
Upon examining the query created by the above HQL, I discover it looks like this:
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
…now, WTF? The select clause aliases don’t exist in the from clause, which explains the error, but not, of course, why the error exists.
How can I work around this?
So here’s the deal: apparently when using HQL, to generate the right query, you need to have the object type you are trying to get as the first part of the query, for example:
This works, where the other query doesn’t. In SQL, the order of the joins doesn’t matter so much; in HQL, it seems quite important.