我正在尝试编写 native 查询:
@NamedNativeQuery(name = "getUncheckedTests",
query = "SELECT t.test_name AS test_name, q.question AS question, @counter \\:= COUNT(ua.is_checked=0) AS uncheckedAnswers FROM user_answer ua" +
" JOIN result r on r.id = ua.result_id" +
" JOIN test t on r.test_id = t.id" +
" JOIN question q on ua.question_id = q.id" +
" WHERE r.is_checked = false AND q.is_open = true AND ua.is_checked = false" +
" AND r.test_id IN (SELECT t.id FROM result r" +
" JOIN test t on t.id = r.test_id" +
" JOIN user u on u.id = r.user_id" +
" WHERE r.permission = 2 AND u.id = :userId) and r.permission = 1 and @counter > 0" +
" GROUP BY ua.question_id")
但是,我得到空结果,因为 @counter > 0
无法正常工作。我可以直接在 Workbench 中执行此查询,但是如果将其作为 native 查询放入 Hibernate 中,则不会有结果
最佳答案
您可能应该检查 Hibernate 实际执行的 SQL enabling logging of the statements或使用 log4jdbc并确保使用正确的参数执行查询。
关于java - hibernate native 查询变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35083840/