org.hibernate.exception.GenericJDBCException:关键字“as”附近的语法不正确
Posted
技术标签:
【中文标题】org.hibernate.exception.GenericJDBCException:关键字“as”附近的语法不正确【英文标题】:org.hibernate.exception.GenericJDBCException: Incorrect syntax near the keyword 'as' 【发布时间】:2021-11-19 18:19:20 【问题描述】:以下是执行分离条件时抛出的异常:
org.springframework.orm.hibernate4.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [n/a]; SQL state [S1000]; error code [156]; Incorrect syntax near the keyword 'as'.; nested exception is org.hibernate.exception.GenericJDBCException: Incorrect syntax near the keyword 'as'.
2021-09-28 20:42:03,882 [mailTimerFactory-1] TRACE org.springframework.transaction.interceptor.TransactionInterceptor - Completing transaction for [com.test.service.impl.MailManagerImpl.processMails]
我已经使用休眠源代码进行了调试,并生成了 sql 查询,因为即使将 showsql 设置为 true,我也无法为条件打印 sql。以下是生成的查询:
[org.hibernate.loader.criteria.CriteriaLoader(select this_.cid as cid9_0_, this_.msgfrom as msgfrom9_0_, this_.msgto as msgto9_0_, this_.msgcc as msgcc9_0_, this_.msubject as msubject9_0_, this_.body as body9_0_, this_.createDate as createDate9_0_, this_.mailDate as mailDate9_0_, this_.expiryDate as expiryDate9_0_, this_.mailsent as mailsent9_0_, this_.remarks as remarks9_0_, this_.html as html9_0_ from mail_queue this_ where this_.mailsent=? and (this_.mailDate is null or this_.mailDate<=?) and (this_.expiryDate is null or this_.expiryDate>=?))]
我已经复制了选择查询,并且能够在 SQL Server 中成功执行它,这让我想知道是什么导致了问题。如果列名包含任何 SQL 关键字,那么它不会被执行,对吧?列名中也没有空格。任何帮助将不胜感激。
编辑:下面是 sql 跟踪:
declare @p1 int
set @p1=NULL
exec sp_prepare @p1 output,N'@P0 bit,@P1 datetime,@P2 datetime,@P3 int,@P4 int',N'WITH query AS (select this_.cid as cid9_0_, this_.ms, ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__gfrom as msgfrom9_0_, this_.msgto as msgto9_0_, this_.msgcc as msgcc9_0_, this_.msubject as msubject9_0_, this_.body as body9_0_, this_.createdate as createdate9_0_, this_.maildate as maildate9_0_, this_.expirydate as expirydate9_0_, this_.mailsent as mailsent9_0_, this_.remarks as remarks9_0_, this_.html as html9_0_ from [mail_queue] this_ where this_.mailsent= @P0 and (this_.maildate is null or this_.maildate<= @P1 ) and (this_.expirydate is null or this_.expirydate>= @P2 )) SELECT * FROM query WHERE __hibernate_row_nr__ >= @P3 AND __hibernate_row_nr__ < @P4 ',1
select @p1
ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__gfrom as msgfrom9_0_
的查询格式不正确。我该如何解决这个问题?事实证明,删除 firstresult 和 maxresults 参数可以解决这个问题:
原创
getHibernateTemplate().findByCriteria(criteria, 0, 250)
删除后
getHibernateTemplate().findByCriteria(criteria)
我相信添加了ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr_
部分是因为添加了 firstresult 和 maxresults 参数,并且 hibernate 使用 rownumber 来过滤记录数。我无法删除限制参数,因为它会获取表中的所有记录。我已经在许多其他分离的标准中使用了这些参数,但奇怪的是在这种情况下它的格式不正确。我该如何解决这个问题?
【问题讨论】:
运行 SQL 跟踪(扩展事件或探查器)以捕获发送到 SQL Server 的实际查询。 谢谢。似乎发送到 SQL 服务器的查询确实不同。我添加了从探查器收到的查询并添加了我的发现。为什么查询格式不正确? 列名“来自”是否是关键字?我找不到相同的参考。同样的 detachedcriteria 在休眠 3 和 spring 3 上运行良好,但在休眠 4 和 spring 4.1.6 上面临上述问题。 这是查询中的问题列,为 2 个AS
子句:`ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__gfrom as msgfrom9_0_
。
是的,hibernate 正在创建查询。生成的查询应该类似于ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr_, this_.msgfrom as msgfrom9_0_
。如何纠正?
【参考方案1】:
看起来像一个错误,但您可以调试 org.hibernate.dialect.pagination.SQLServer2005LimitHandler#processSql
以查看问题所在,甚至应用自定义解决方法,因为您可以在自定义 Dialect
中替换 LimitHandler
。
【讨论】:
以上是关于org.hibernate.exception.GenericJDBCException:关键字“as”附近的语法不正确的主要内容,如果未能解决你的问题,请参考以下文章