如果没有活动事务

如果没有活动事务

本文介绍了Org.hibernate.HibernateException:如果没有活动事务,createQuery无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 继此问题,我想添加Hibernate 4.3.8.Final到Spring 4.1.5.RELEASE;但 - 代码抛出一个异常,如下面的,有时抛出 - 虽然该类存在,但它无法找到hibernate的实体类(com.myproject.model.MyTable)位于com.myproject.model包中。 StackTrace org.hibernate.HibernateException:的createQuery也不是没有在org.hibernate.context.internal.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java活动事务有效的: 352) at com.sun.proxy $ Proxy40.createQuery(Unknown Source) at com.myproject.repository.TestRepImpl.get(TestRepImpl.java:21) at sun.reflect .NativeMethodAccessorImpl.invoke0(本地方法) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java。 lang.reflect.Method.invoke(Method.java:483)处org.springframework.aop.framework org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)。 ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke( PersistenceExceptionTranslationInterceptor.java:136)在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179 )在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java: 207) at com.sun.proxy $ Proxy33.get(Unknown Source) at com.myproject.service.TestServiceImpl.get(TestServiceImpl.java:17) at sun.reflect .NativeMethodAccessorImpl.invoke0(本地方法) at sun.reflect.NativeMethodA ccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)$ b $在org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99) b的组织。 springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)在org.springframework.aop。 framework.Re flectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy。$ Proxy35.get(Unknown源代码) at com.myproject.web.MainController.index(MainController.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl。调用(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.invoke(Method.java:483)。在org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:177)在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java: 446)在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handl e(AnnotationMethodHandlerAdapter.java:434) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet。 Java的:877),美元,org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966 b $ b)在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:620)在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) 。在使用javax .servlet.http.HttpServlet.service(HttpServlet.java:727)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 在org.apache.catalina.core .ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache .catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)在org.apache.catalina.core .StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:504)在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java :103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) at org.apache.coyote.http11.AbstractHttp11Processor.process(Ab stractHttp11Processor.java:1074)在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:611)在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run( JIoEndpoint.java:314)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) pr-servlet.xml <?xml version =1.0encoding =UTF-8?> < beans xmlns:mvc =http://www.springframework.org/schema/mvc xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance xmlns =http://www.springframework.org/schema/beans xmlns:context =http://www.springframework.org/schema/contextxmlns:tx =http:// www.springframework.org/schema/tx xsi:schemaLocation =http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc。 xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org / schema / tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/模式/上下文/弹簧上下文4.1.xsd> < context:annotation-config /> < context:component-scan base-package =com.myproject/> < bean id =dataSourceclass =org.apache.commons.dbcp2.BasicDataSource destroy-method =close> < property name =driverClassNamevalue =com.mysql.jdbc.Driver/> < property name =urlvalue =jdbc:mysql:// localhost:8889 / MyProject/> < property name =usernamevalue =jack/> < property name =passwordvalue =jack/> < / bean> < bean id =sessionFactory class =org.springframework.orm.hibernate4.LocalSessionFactoryBean> < property name =dataSourceref =dataSource/> < property name =configLocationvalue =classpath:hibernate.cfg.xml/> < property name =packagesToScanvalue =com.myproject.model/> < / bean> < bean class =org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor/> < bean id =transactionManager class =org.springframework.orm.hibernate4.HibernateTransactionManager> < property name =sessionFactoryref =sessionFactory/> < / bean> < tx:注解驱动的事务管理器=transactionManager/> < / beans> hibernate.cfg.xml <?xml version ='1.0'encoding ='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate配置DTD 3.0 // ENhttp://www.hibernate.org/dtd/hibernate-configuration -3.0.dtd> < hibernate-configuration> < session-factory> < property name =connection.pool_size> 12< / property> < property name =dialect> org.hibernate.dialect.MySQLDialect< / property> < property name =current_session_context_class>线程< / property> < property name =show_sql> true< / property> < property name =hbm2ddl.auto>建立< / property> < mapping class =com.myproject.model.MyTable/> < / session-factory> < / hibernate-configuration> MainController @Controller public class MainController { @Autowired private TestService testService; $ b $ @ @RequestMapping(/ {viewName} .htm) public String index(@PathVariable(value =viewName)String viewName){ System.err.println (的viewName); testService.get(1); if(isValidView(viewName)){ return viewName; } 返回null; } TestServiceImpl $ b @Service public class TestServiceImpl implements TestService { @Autowired private TestRepDao testDao; @Transactional public void get(int i){ testDao.get(i); } } TestRepImp @Repository public class TestRepImpl implements TestRepDao { $ b $ @Autowired SessionFactory sessionFactory; public void get(int id){ String hql =from MyTable where id =+ id; Query query = sessionFactory.getCurrentSession()。createQuery(hql); @SuppressWarnings(unchecked) List< MyTable> listBusiness =(List< MyTable>)query.list(); System.err.println(listBusiness.get(0)); $ b 解决方案首先,你应该删除你的hibernate.cfg.xml文件。通过使用< property name =packagesToScanvalue =com.myproject.model/> ,您告诉Hibernate扫描您的Java类以获得JPA注释以构建作为一个副作用,这也将删除< property name =dialect> org.hibernate行。 dialect.MySQLDialect< / property> 需要出来,因为Hibernate可以选择它自己的方言,如果你强制使用方言,它可能会导致各种奇怪的错误。我从来没有遇到Hibernate为我选择方言的问题。 如果你有任何一种说的异常,当'hibernate.dialect'没有设置时连接不能为空,这意味着Hibernate实际上无法连接到数据库。 Hibernate会话上下文管理是Spring不需要的东西,因为Spring正在控制会话上下文。 您的例外情况无法找到antlr和 java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener 就像我们在聊天中讨论的那样,是因为你没有使用依赖管理器来引入Spring / Hibernate依赖的类。 在正确的轨道上,使用Spring来管理你的会话。会话未被打开的问题是由于 transactionManager 和 sessionFactory 被错误配置。我建议你用下面的代码替换你的pr-servlet: $ b <?xml version = 1.0encoding =UTF-8?> < beans xmlns =http://www.springframework.org/schema/beans xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance xmlns:jdbc =http://www.springframework.org/schema/jdbc xmlns:tx =http://www.springframework.org/schema/tx xmlns :context =http://www.springframework.org/schema/context xmlns:mvc =http://www.springframework.org/schema/mvc xsi:schemaLocation = http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/ mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema /beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2。 XSD> < tx:annotation-driven /> < context:component-scan base-package =com.project/> < mvc:annotation-driven /> < bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSource destroy-method =close> < property name =driverClassNamevalue =com.mysql.jdbc.Driver/> < property name =urlvalue =jdbc:mysql:// localhost:8889 / MyProject/> < property name =usernamevalue =jack/> < property name =passwordvalue =jack/> < / bean> < bean id =sessionFactory class =org.springframework.orm.hibernate4.LocalSessionFactoryBean> < property name =dataSourceref =dataSource/> < property name =packagesToScanvalue =com.project.model/> < property name =hibernateProperties> <道具> < prop key =hibernate.format_sql> true< / prop> < prop key =hibernate.use_sql_comments> true< / prop> < prop key =hibernate.hbm2ddl.auto>验证< / prop> < /道具> < / property> < / bean> < bean class =org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor id =PersistenceExceptionTranslator/> < bean id =transactionManager class =org.springframework.orm.hibernate4.HibernateTransactionManager> < property name =sessionFactoryref =sessionFactory/> < / bean> < / beans> 让您的交易管理员工作。 Following this question, I am trying to add Hibernate 4.3.8.Final to Spring 4.1.5.RELEASE; but- The code throws an exception as following and at times throws- an exception that it cannot find entity class of hibernate (com.myproject.model.MyTable) although the class exists and is located in com.myproject.model package.StackTraceorg.hibernate.HibernateException: createQuery is not valid without active transaction at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) at com.sun.proxy.$Proxy40.createQuery(Unknown Source) at com.myproject.repository.TestRepImpl.get(TestRepImpl.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy33.get(Unknown Source) at com.myproject.service.TestServiceImpl.get(TestServiceImpl.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy35.get(Unknown Source) at com.myproject.web.MainController.index(MainController.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:177) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)pr-servlet.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"><context:annotation-config /><!-- <mvc:annotation-driven /> --><context:component-scan base-package="com.myproject" /><!-- viewResolver configuration omitted --><bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:8889/MyProject" /> <property name="username" value="jack" /> <property name="password" value="jack" /></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="packagesToScan" value="com.myproject.model" /></bean><bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /></bean><tx:annotation-driven transaction-manager="transactionManager" /></beans>hibernate.cfg.xml<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">12</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping class="com.myproject.model.MyTable" /> </session-factory></hibernate-configuration>MainController@Controllerpublic class MainController { @Autowired private TestService testService; @RequestMapping("/{viewName}.htm") public String index(@PathVariable(value = "viewName") String viewName) { System.err.println(viewName); testService.get(1); if (isValidView(viewName)) { return viewName; } return null; }TestServiceImpl@Servicepublic class TestServiceImpl implements TestService { @Autowired private TestRepDao testDao; @Transactional public void get(int i) { testDao.get(i); }}TestRepImp@Repositorypublic class TestRepImpl implements TestRepDao { @Autowired SessionFactory sessionFactory; public void get(int id) { String hql = "from MyTable where id=" + id; Query query = sessionFactory.getCurrentSession().createQuery(hql); @SuppressWarnings("unchecked") List<MyTable> listBusiness = (List<MyTable>) query.list(); System.err.println(listBusiness.get(0)); }} 解决方案 Firstly, you should remove your hibernate.cfg.xml. By using <property name="packagesToScan" value="com.myproject.model" /> you are telling Hibernate to scan your Java classes for JPA annotations to build the database model with.As a side effect, this will also remove the line <property name="dialect">org.hibernate.dialect.MySQLDialect</property> which needs to come out because Hibernate can select it's own dialect, and if you force a dialect it can cause all kinds of weird errors. I've never had a problem with Hibernate selecting a dialect for me. If you ever get any kind of exception that says Connection cannot be null when 'hibernate.dialect' is not set, that means that Hibernate couldn't actually connect to your database.Hibernate session context management is something that you won't need with Spring, as Spring is controlling the session context.Your exception about the cannot find antlr and java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener as we discussed in chat, is because you were not using a dependency manager to pull in the classes that Spring/Hibernate were depending on.You are certainly on the right track, using Spring to manage your sessions. The issue with the session not being open was due to the transactionManager and sessionFactory being mis-configured. I suggested you replace your pr-servlet with the following:<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <tx:annotation-driven /> <context:component-scan base-package="com.project"/> <mvc:annotation-driven/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:8889/MyProject" /> <property name="username" value="jack" /> <property name="password" value="jack" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.project.model" /> <property name="hibernateProperties"> <props> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.use_sql_comments">true</prop> <prop key="hibernate.hbm2ddl.auto">validate</prop> </props> </property> </bean> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" id="PersistenceExceptionTranslator" /> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean></beans>which got your transaction manager working. 这篇关于Org.hibernate.HibernateException:如果没有活动事务,createQuery无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-14 20:25