我正在尝试使用 sqlserver 2008 作为 DB 将应用程序从 jboss 4.2.3 移植到 7.1.1,当我在 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();
任何帮助将不胜感激。