MSSQL使用DISTINCT时Hibernate语法无效? [英] MSSQL Invalid Syntax with Hibernate when using DISTINCT?
本文介绍了MSSQL使用DISTINCT时Hibernate语法无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最终查询查询= session.createSQLQuery(SELECT DISTINCT(expense_document。 id)FROM expense_document JOIN generic_object ON expense_document.id = generic_object.id JOIN expense_document_item ON expense_document_item.document_id = expense_document.id JOIN generic_object ON expense_document_item.id = generic_object.id WHERE expense_document.client_id =:client_id)。setParameter(client_id, 。client.getId())setMaxResults(1000);
当执行这段代码时,我得到:
org.hibernate.exception.SQLGrammarException:无法执行查询
导致:com.microsoft.sqlserver.jdbc.SQLServerException:关键字'from'附近的语法不正确。
我无法找到MS SQL对此查询不喜欢的内容。当我连接到MySQL时,这条线不会引起任何问题。
解决方案 尝试删除参数周围的圆括号到 distinct
关键字。
像这样使用它:
从某处选择不同的东西
So I have this piece of Java code:
final Query query = session.createSQLQuery("SELECT DISTINCT(expense_document.id) FROM expense_document JOIN generic_object ON expense_document.id = generic_object.id JOIN expense_document_item ON expense_document_item.document_id = expense_document.id JOIN generic_object ON expense_document_item.id = generic_object.id WHERE expense_document.client_id = :client_id").setParameter("client_id", client.getId()).setMaxResults(1000);
and when this code is executed I get:
org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'from'.
I can not find what MS SQL does not like about this query. When I am connected to MySQL, this line will not cause any problems.
解决方案
Try removing the parenthesis around the parameter to the distinct
keyword.
Use it like this:
select distinct something from somewhere
这篇关于MSSQL使用DISTINCT时Hibernate语法无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文