4

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Soosh
  • 812
  • 1
  • 8
  • 24
  • did you resolved this trouble? I am having now the same situation and I don't know how to fix it. – Filosssof Oct 24 '16 at 14:07
  • @Filosssof, I could not solve the problem but I used a workaround which I do not remember (sorry)! anyway, I suggest to first update your hibernate and second change your query like not using the `setFirstResult` method and put the required info in your query. – Soosh Nov 30 '16 at 14:24
  • I didn't resolved this trouble. The work around was to downgrade version of hibernate to the previous one. But this isn't a good way for me. – Filosssof Dec 02 '16 at 12:17
  • @Filosssof hmmmm, downgrade! :D sorry bro. – Soosh Dec 05 '16 at 14:11

0 Answers0