4

我试图从带有本机查询的表中进行选择,如下所示:

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'

这很好,但是当我将 更改为firstIndex0 以外的值时,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(); 

谁能告诉我怎么了?这是一个错误还是我做错了什么?

4

0 回答 0