问题描述
applicationContext.xml<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"><beanclass="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /><!-- 配置Jpa --><bean id="entityManagerFactory"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="dataSource" ref="dataSource" /><property name="jpaVendorAdapter"><bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"><property name="database" value="MYSQL" /><property name="showSql" value="true" /></bean></property><!-- 根据实体Bean自动生成表 --><property name="jpaProperties"><props><prop key="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!-- 配置数据源 --><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property><property name="url"value="jdbc:sqlserver://172.16.1.48:1433;instanceName=sql08;databaseName=YXTong_BZ"></property><property name="username" value="sa" /><property name="password" value="sa123" /></bean><!-- 配置事务 --><bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"><property name="entityManagerFactory" ref="entityManagerFactory" /></bean><tx:annotation-driven transaction-manager="transactionManager" /></beans>查询代码:Query query = this.entityManager.createQuery("select p FROM ACase p order by p.id desc");query.setFirstResult(0);query.setMaxResults(10);List<ACase> list = query.getResultList();输出错误:Hibernate: select acase0_.id as id0_, acase0_.addTime as addTime0_, acase0_.caseName as caseName0_ from ACase acase0_ order by acase0_.id desc limit ?Dao------------getACaseByHqlAppendParameter---------org.hibernate.exception.SQLGrammarException: could not execute queryService------------getACaseByHqlAppendParameter---------Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly2013-9-3 17:15:20 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() for servlet default threw exceptionjavax.persistence.RollbackException: Transaction marked as rollbackOnlyat org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:51)at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:456)at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:709)at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:678)at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:359)at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)at $Proxy12.getACaseByHqlAppendParameter(Unknown Source)at com.ct.yxtbz.service.impl.ACaseService.getACaseByHqlAppendParameter(ACaseService.java:185)at com.ct.yxtbz.web.action.ACaseAction.acaseList(ACaseAction.java:99)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)不知道是配置错了。。还是怎么了。。。若是查询时不为Query对象加setFirstResult和setMaxResults的话,就不会报错。能查出数据。
解决方案
下面这段有问题,database属性为SQL_SERVER引用<property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="MYSQL" /> <property name="showSql" value="true" /> </bean> </property>