我试图从具有原生查询的表中进行选择,如下所示:
entityManager.createNativeQuery("SELECT * FROM B_AccountTransaction " +
"accountTransaction WHERE accountTransaction.depositId = '" + depositId + "'")
.setFirstResult(firstIndex).setMaxResults(pageSize).getResultList();
当firstIndex
为0(与pageSize
无关)时,Hibernate会为我创建以下查询:
SELECT
TOP(?) *
FROM
B_AccountTransaction accountTransaction
WHERE
accountTransaction.depositId = '10002801130'
这很好但是当我将firstIndex
更改为0以外的值时,hibernate会为我创建以下查询:
WITH query AS
( SELECT
inner_query.*,
ROW_NUMBER() OVER ( ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__
FROM
( SELECT * as page0_
FROM
B_AccountTransaction accountTransaction
WHERE
accountTransaction.depositId = '10002801130'
) inner_query
)
SELECT
page0_
FROM
query
WHERE
__hibernate_row_nr__ >= ? -- firstIndex here
AND __hibernate_row_nr__ < ? -- page size here
但此查询包含SELECT * as page0_
,这不是有效的SQL Server语法。
另请注意,当我使用以下代码时,一切都很好:
entityManager.createNativeQuery("SELECT accountTransaction.AccountTransactionID as AccountTransactionID, accountTransaction.AccountNo as AccountNo, " +
"accountTransaction.TransactionNumber as TransactionNumber, accountTransaction.TransactionDate as TransactionDate, accountTransaction.TransactionTime as TransactionTime," +
"accountTransaction.TransactionTimeStamp as TransactionTimeStamp, accountTransaction.DepositID as DepositID, accountTransaction.BranchCode as BranchCode, " +
"accountTransaction.DebitAmount as DebitAmount, accountTransaction.CreditAmount as CreditAmount, accountTransaction.Balance as Balance, accountTransaction.PayerName as PayerName, " +
"accountTransaction.TransDesc as TransDesc, accountTransaction.ChqNumber as ChqNumber, accountTransaction.TransactionLastRowNumber as TransactionLastRowNumber, accountTransaction.ChqSeri as ChqSeri, " +
"accountTransaction.DocType as DocType, accountTransaction.TransactionID as TransactionID, accountTransaction.EffectiveDate as EffectiveDate " +
" FROM B_AccountTransaction accountTransaction WHERE accountTransaction.depositId = '" + depositId + "'")
.setFirstResult(firstIndex).setMaxResults(pageSize).getResultList();
谁能告诉我什么错了?这是一个错误还是我做错了什么?