CreateQuery 在没有活动事务的情况下无效
问题:CreateQuery 在没有活动事务的情况下无效 我正在尝试使用 Spring @Transactional 注释,但是当调用 findAll 方法时出现问题并且出现此错误: org.hibernate.HibernateException: createQuery is not valid without active transaction at org.hibernate.conte
·
问题:CreateQuery 在没有活动事务的情况下无效
我正在尝试使用 Spring @Transactional 注释,但是当调用 findAll 方法时出现问题并且出现此错误:
org.hibernate.HibernateException: createQuery is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy57.createQuery(Unknown Source)
at org.munaycoop.taskmanager.daos.PersonDataAccesObject.findAll(PersonDataAccesObject.java:20)
at org.munaycoop.taskmanager.services.PersonService.findAll(PersonService.java:26)
at org.munaycoop.taskmanager.controllers.PersonController.ShowAllPersons(PersonController.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
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:707)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:440)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
个人 DAO:
@Repository
@Transactional
public class PersonDataAccesObject implements IPersonDataAccesObject {
@Autowired
private SessionFactory session;
@Override
public List findAll() {
return session.getCurrentSession().createQuery("from persons").list();
}
这是我的servlet-context.xml
:
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/bootstrap/" />
<!-- Database Configuration -->
<context:property-placeholder location="classpath:database.properties" />
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="${jdbc.driverClassName}" />
<beans:property name="url" value="${jdbc.databaseUrl}" />
<beans:property name="username" value="${jdbc.username}" />
<beans:property name="password" value="${jdbc.password}" />
</beans:bean>
<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.connection.pool_size">${hibernate.pool_size}</beans:prop>
<beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
<beans:prop key="hibernate.current_session_context_class">${hibernate.current_session}</beans:prop>
<beans:prop key="hibernate.cache.provider_class">${hibernate.provider_class}</beans:prop>
<beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl}</beans:prop>
</beans:props>
</beans:property>
<beans:property name="mappingResources">
<beans:list>
<beans:value>hibernate.cfg.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<!-- Hibernate TransactionManager -->
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>
我尝试删除此行<beans:prop key="hibernate.current_session_context_class">${hibernate.current_session}</beans:prop>
,但又出现了另一个错误,为什么这不起作用?
解答
您必须明确声明对事务管理器注释的支持
添加到您的配置中:
<tx:annotation-driven transaction-manager="transactionManager"/>
tx 是 xmlns:txu003d"http://www.springframework.org/schema/tx" 命名空间。
并删除这一行:
<beans:prop key="hibernate.current_session_context_class">${hibernate.current_session}</beans:prop>
更多推荐
已为社区贡献23584条内容
所有评论(0)