【发布时间】:2016-05-27 15:21:48
【问题描述】:
我目前正在使用 JPQL 查询从数据库中检索信息。该项目的目的是通过具有不同元素的随机性来测试示例环境,因此我需要查询来检索整个项目中的随机单个结果。
我面临 JPQL 没有为随机检索实现适当的函数,并且随机后计算需要太长时间(附加函数返回随机结果需要 14 秒)
public Player getRandomActivePlayerWithTransactions(){
List<Player> randomPlayers = entityManager.createQuery("SELECT pw.playerId FROM PlayerWallet pw JOIN pw.playerId p"
+ " JOIN p.gameAccountCollection ga JOIN ga.iDAccountStatus acs"
+ " WHERE (SELECT count(col.playerWalletTransactionId) FROM pw.playerWalletTransactionCollection col) > 0 AND acs.code = :status")
.setParameter("status", "ACTIVATED")
.getResultList();
return randomPlayers.get(random.nextInt(randomPlayers.size()));
}
由于 JPQL 限制,不允许使用 ORDER BY NEWID(),我测试了以下内联条件,所有这些条件在编译时都返回语法错误。
WHERE (ABS(CAST((BINARY_CHECKSUM(*) * RAND()) as int)) % 100) < 10
WHERE Rnd % 100 < 10
FROM TABLESAMPLE(10 PERCENT)
【问题讨论】:
-
JPQL 允许“FUNCTION(...)”使用原生 SQL 函数。但是 JPQL 的语法在很多地方都有定义,例如 datanucleus.org/products/accessplatform_4_2/jpa/jpql.html#bnf 所以你为什么只是转储大量 SQL 并期望它能够工作我不知道
标签: sql-server jpa jpql