更改为Sqlserver2008dialect时,本机Sql无法按预期工作

时间:2013-10-13 12:24:26

标签: java sql-server-2008 hibernate jboss7.x

我正在尝试将应用程序从jboss 4.2.3移植到7.1.1,sqlserver 2008作为数据库,当我在persistence.xml中将org.hibernate.dialect.SQLServerDialect的方言更改为2008方言时,我遇到了问题。对于以前的版本方言,我遇到了分页问题,​​其中提供了改变方言作为解决方案。从日志中我分析了一些自动添加的其他语句。

版本详情如下

16:46:10,844 INFO  [org.hibernate.annotations.common.Version] (MSC service thread 1-5) HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
16:46:10,849 INFO  [org.hibernate.Version] (MSC service thread 1-5) HHH000412: Hibernate Core {4.0.1.Final}
16:46:11,870 INFO  [org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator] (MSC service thread 1-5) HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
16:46:11,892 INFO  [org.hibernate.dialect.Dialect] (MSC service thread 1-5) HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
16:46:11,905 INFO  [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (MSC service thread 1-5) HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
16:46:11,908 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (MSC service thread 1-5) HHH000397: Using ASTQueryTranslatorFactory
16:46:11,934 INFO  [org.hibernate.validator.util.Version] (MSC service thread 1-5) Hibernate Validator 4.2.0.Final

实际查询

select proj.proj_id,count(distinct projbid.user_id)  as numRespondents,c.customer_name,proj.campaign_id from  customer c,proj left join  projbid ON  (projbid.proj_id = proj.proj_id AND projbid.bid_status in (2, 5)) where proj.spec_id=spec.spec_id and proj.userid= :userId and proj.int_client_id=c.customer_id and proj.proj_state= :projState group by proj.proj_id, c.customer_name,proj.campaign_id  order by proj.job_id ASC 

JPA / Hibernate使用的查询

16:47:39,206 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) Hibernate: 
16:47:39,207 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)     /* dynamic native SQL query */ WITH query AS (select
16:47:39,208 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         proj.proj_id,
16:47:39,213 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         count(bid.user_id)  numrespondents,
16:47:39,214 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         c.customer_name,
16:47:39,215 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         proj.campaign_id,
16:47:39,216 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         ROW_NUMBER() OVER (
16:47:39,217 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)     order by
16:47:39,218 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         proj.proj_id asc) as __hibernate_row_nr__ 
16:47:39,219 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)     from
16:47:39,221 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         customer c,
16:47:39,222 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         proj  
16:47:39,223 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)     left join
16:47:39,223 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         projbid 
16:47:39,224 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)             on  (projbid.proj_id = proj.proj_id 
16:47:39,225 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)             and projbid.bid_status in (2,
16:47:39,225 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         5)) 
16:47:39,226 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)     where
16:47:39,227 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         proj.spec_id=proj.spec_id 
16:47:39,227 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         and 
16:47:39,228 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         and proj.userid= ? 
16:47:39,229 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         and proj.int_client_id=c.customer_id 
16:47:39,230 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         and proj.proj_state= ? 
16:47:39,231 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)     group by
16:47:39,231 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         proj.proj_id,
16:47:39,234 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         job.userid,
16:47:39,236 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         c.customer_name,
16:47:39,237 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         proj.campaign_id   
*16:47:39,238 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)     group by
16:47:39,239 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         proj.proj_id,
16:47:39,243 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         count(projbid.user_id)    numrespondents,
16:47:39,244 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         c.customer_name,
16:47:39,245 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         proj.campaign_id )* SELECT
16:47:39,246 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         * 
16:47:39,246 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)     FROM
16:47:39,247 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         query 
16:47:39,248 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)     WHERE
16:47:39,248 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         __hibernate_row_nr__ >= ? 
16:47:39,249 INFO  [stdout] (http-localhost-127.0.0.1-8080-2)         AND __hibernate_row_nr__ < ?


**I am getting the exception as below** 

16:47:39,262 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-2) SQL Error: 156, SQLState: S0001
16:47:39,264 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-2) Incorrect syntax near the keyword 'group'.

我可以看到groupby被多次添加,而执行的java片段在

之下
results = em.createNativeQuery(selectSql.toString()).setParameter("userId", userId).setParameter("projState", 2).setMaxResults(param.getPageSize()).setFirstResult(param.getStart()).setHint("org.hibernate.readOnly", true).getResultList();

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

AS 7.1.1.Final附带的hibernate中存在一个错误。 它是在不久前修复的。 您可以通过

解决此问题
  • 使用较新版本的AS,我建议7.2.0.Final(作为EAP 6.1.Alpha发布)
  • 手动将hibernate升级到4.2.x系列中的最新版本(目前为4.2.6.Final)
  • 通过将方言设置为SQLServer2005(其行为与2008年大致相同)
  • 来解决此问题

短期我只想将方言改为2005年或将AS升级到7.2