我现在正在谷歌搜索2天,但我无法找到适合我的问题的答案: - )。
我尝试使用Spring Data JPA Repositories findAll()
函数来使用分页。
我在版本" 1.7.1.RELEASE"中使用Spring-Data-JPA。 后端SQL是MS-SQL 2008服务器,我在版本" 1.3.1"中使用JTDS。
我创建了一个JPA实体并使用例如findOne
方法查询数据库工作正常并给出了预期的答案但是当我使用findAll(Pageable arg0)
方法时,抛出了以下的栈跟踪:
2015-01-26 12:23:49,527 DEBUG | could not extract ResultSet [n/a]
java.sql.SQLException: Incorrect syntax near 'limit'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:505) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:1029) ~[jtds-1.3.1.jar:1.3.1]
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76) ~[c3p0-0.9.1.2.jar:0.9.1.2]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.Loader.getResultSet(Loader.java:2066) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1863) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:910) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2554) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2540) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.Loader.list(Loader.java:2365) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573) [hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449) [hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67) [hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.readPage(SimpleJpaRepository.java:459) [spring-data-jpa-1.7.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:357) [spring-data-jpa-1.7.1.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:326) [spring-data-jpa-1.7.1.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_75]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_75]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_75]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_75]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:442) [spring-data-commons-1.9.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:427) [spring-data-commons-1.9.1.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:381) [spring-data-commons-1.9.1.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) [spring-tx-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) [spring-tx-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) [spring-tx-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) [spring-tx-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122) [spring-data-jpa-1.7.1.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at com.sun.proxy.$Proxy57.findAll(Unknown Source) [na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_75]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_75]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_75]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_75]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) [spring-tx-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) [spring-tx-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) [spring-tx-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at com.sun.proxy.$Proxy58.findAll(Unknown Source) [na:na]
at com.X.Y.Z.main(GenericAccountCompare_Analyse.java:51) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_75]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_75]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_75]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_75]
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293) [exec-maven-plugin-1.3.2.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_75]
2015-01-26 12:23:49,528 WARN | SQL Error: 102, SQLState: 42000
2015-01-26 12:23:49,528 ERROR | Incorrect syntax near 'limit'.
2015-01-26 12:23:49,529 DEBUG | Mark transaction for rollback
2015-01-26 12:23:49,531 DEBUG | rolling back
2015-01-26 12:23:49,559 DEBUG | rolled JDBC Connection
2015-01-26 12:23:49,559 DEBUG | re-enabling autocommit
2015-01-26 12:23:49,588 DEBUG | Releasing JDBC connection
2015-01-26 12:23:49,588 DEBUG | Released JDBC connection
我的VendorAdapterConfig看起来像
<bean id="jpaVendor"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServer2008Dialect" />
<property name="database" value="SQL_SERVER" />
</bean>
以下是使用findAll方法的代码块(stacktrace中所述的第51行):
Page<Workrelationship> wrPage = wrRepo.findAll(new PageRequest(0, 20));
任何想法我做错了什么?
谢谢和亲切的问候
Shelltux
PS:我也使用具有相同错误的MS-JDBC驱动程序对其进行了测试
[更新]
以下是生成的查询:
select workrelati0_.a as aR1_9_, workrelati0_.b as b2_9_, workrelati0_.c as c6_9_, workrelati0_.d as d7_9_, workrelati0_.e as e3_9_, workrelati0_.f as f4_9_, workrelati0_.g as gO5_9_ from tblWorkRelationship workrelati0_ limit ?
如果我直接使用MS-SQL Studio执行该查询,则收到相同的错误 语法不正确接近&#39;限制&#39;。
我没有persistence.xml或任何其他hibernate配置。所有这些都是使用Spring-Data Configbeans完成的:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:property-placeholder location="classpath:application.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="acquireIncrement" value="${jdbc.c3p0.pool.acquireIncrement}" />
<property name="minPoolSize" value="${jdbc.c3p0.pool.minPoolSize}" />
<property name="maxPoolSize" value="${jdbc.c3p0.pool.maxPoolSize}" />
</bean>
<jpa:repositories base-package="com.X.Y.Z.repository"
entity-manager-factory-ref="myEmf"
transaction-manager-ref="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
<property name="persistenceUnitName" value="base" />
<qualifier value="my-db" />
</bean>
<bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendor" />
<property name="packagesToScan" value="com.X.Y.Z.domain" />
<property name="persistenceUnitName" value="base" />
</bean>
<bean id="jpaVendor"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServer2008Dialect" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
[/更新]
[UPDATE2]
我使用supportsLimit-Function测试了方言,结果意外。 如上所述,我使用的是MS-SQL 2008服务器,从MS-SQL 2012开始支持limit-Function。
SQLServerDialect,以及SQLServer2005Dialect以及SQLServer2008Dialect通过调用supportLimit返回true。
SQLServerDialect dialect = new SQLServerDialect();
log.info("Support limit: " + dialect.supportsLimit());
SQLServer2005Dialect dialect2005 = new SQLServer2005Dialect();
log.info("Support limit: " + dialect2005.supportsLimit());
SQLServer2008Dialect dialect2008 = new SQLServer2008Dialect();
log.info("Support limit: " + dialect2008.supportsLimit());
2015-01-26 13:52:38,594 INFO | HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
2015-01-26 13:52:38,596 INFO | Support limit: true
2015-01-26 13:52:38,597 INFO | HHH000400: Using dialect: org.hibernate.dialect.SQLServer2005Dialect
2015-01-26 13:52:38,597 INFO | Support limit: true
2015-01-26 13:52:38,598 INFO | HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
2015-01-26 13:52:38,599 INFO | Support limit: true
[/ UPDATE2]
[UPDATE3]
这是输出,包括getLimitString:
2015-01-26 14:25:14,815 INFO | HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
2015-01-26 14:25:14,817 INFO | Support limit: true
2015-01-26 14:25:14,817 INFO | Limit-String: select top 20 * from A
2015-01-26 14:25:14,817 INFO | HHH000400: Using dialect: org.hibernate.dialect.SQLServer2005Dialect
2015-01-26 14:25:14,818 INFO | Support limit: true
2015-01-26 14:25:14,818 INFO | Limit-String: select top 20 * from A
2015-01-26 14:25:14,818 INFO | HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
2015-01-26 14:25:14,819 INFO | Support limit: true
2015-01-26 14:25:14,820 INFO | Limit-String: select top 20 * from A
对我来说是正确的: - )。
[/ UPDATE3]
答案 0 :(得分:1)
我也使用了2个实体管理器,但我已经将一个(MySQL)的数据库方言复制并粘贴到另一个(SQLServer)并且遇到了同样的问题。这篇文章很棒,因为我检查了我的数据库方言,确定它不正确。一旦纠正,它就有效了!
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServer2012Dialect");
答案 1 :(得分:0)
感谢您的评论。 我发现了错误: - )
这是我的错。 我正在使用两个带JPA的Transaction-Managers。一个用于应用程序后端SQL,另一个用于嵌入式数据库。 所以我不得不配置两个Spring HibernateJpaVendorAdapter Beans,故障是我给了两个相同的名称,因此应用程序后端的那个被嵌入式数据库覆盖了。
谢谢和问候
Shelltux
答案 2 :(得分:0)
对于后代: 这为我解决了这个问题:
在application.yml中:
datasources:
thisIsTheDatasourceName:
dialect: org.hibernate.dialect.SQLServer2012Dialect