I was trying to select from table with native query as follows:
entityManager.createNativeQuery("SELECT * FROM B_AccountTransaction " +
"accountTransaction WHERE accountTransaction.depositId = '" + depositId + "'")
.setFirstResult(firstIndex).setMaxResults(pageSize).getResultList();
When the firstIndex
is 0 (does not matter what pageSize
is), Hibernate create the following query for me:
SELECT
TOP(?) *
FROM
B_AccountTransaction accountTransaction
WHERE
accountTransaction.depositId = '10002801130'
which is fine but when I change the firstIndex
to a value other than 0, hibernate will create the following query for me:
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
but this query contains SELECT * as page0_
which is not a valid SQL Server syntax.
Also please note that when I use the following code, everything is fine:
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();
Can anyone tell me whats wrong? Is this a bug or I'm doing something wrong?