更新:抱歉,我似乎忘记了一些事情......请参阅调度员servlet.xml的帖子底部以及调用@Repository的bean:
我似乎无法让这个因某些原因而起作用。我在em.flush()调用中不断收到以下异常:
这是例外:
2011-08-29 00:29:32,142 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <DispatcherServlet with name 'dispatcher' processing GET request for [/orphanware.com/]>
2011-08-29 00:29:32,142 DEBUG [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - <Mapping [/] to HandlerExecutionChain with handler [com.orphanware.web.controller.HomeController@55ca6954] and 2 interceptors>
2011-08-29 00:29:32,143 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Last-Modified value for [/orphanware.com/] is: -1>
2011-08-29 00:29:32,143 DEBUG [org.springframework.web.bind.annotation.support.HandlerMethodInvoker] - <Invoking request handler method: public java.lang.String com.orphanware.web.controller.HomeController.index(java.util.Map)>
2011-08-29 00:29:32,144 DEBUG [org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler] - <Creating new EntityManager for shared EntityManager invocation>
2011-08-29 00:29:32,144 DEBUG [org.hibernate.impl.SessionImpl] - <opened session at timestamp: 13146029721>
2011-08-29 00:29:32,144 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - <generated identifier: 111, using strategy: org.hibernate.id.SequenceHiLoGenerator>
2011-08-29 00:29:32,145 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - <Closing JPA EntityManager>
2011-08-29 00:29:32,145 DEBUG [org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler] - <Creating new EntityManager for shared EntityManager invocation>
2011-08-29 00:29:32,146 DEBUG [org.hibernate.impl.SessionImpl] - <opened session at timestamp: 13146029721>
2011-08-29 00:29:32,146 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - <Closing JPA EntityManager>
2011-08-29 00:29:32,147 DEBUG [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver] - <Resolving exception from handler [com.orphanware.web.controller.HomeController@55ca6954]: javax.persistence.TransactionRequiredException: no transaction is in progress>
2011-08-29 00:29:32,147 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] - <Resolving exception from handler [com.orphanware.web.controller.HomeController@55ca6954]: javax.persistence.TransactionRequiredException: no transaction is in progress>
2011-08-29 00:29:32,147 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] - <Resolving exception from handler [com.orphanware.web.controller.HomeController@55ca6954]: javax.persistence.TransactionRequiredException: no transaction is in progress>
2011-08-29 00:29:32,148 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Could not complete request>
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
at $Proxy15.flush(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy15.flush(Unknown Source)
at com.orphanware.web.dao.SampleDao.saveSample(SampleDao.java:22)
at com.orphanware.web.services.SampleService.saveSample(SampleService.java:23)
at com.orphanware.web.controller.HomeController.index(HomeController.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.bufferAndPostProcess(ContentBufferingFilter.java:169)
at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.doFilter(ContentBufferingFilter.java:126)
at org.sitemesh.config.ConfigurableSiteMeshFilter.doFilter(ConfigurableSiteMeshFilter.java:157)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Aug 29, 2011 12:29:32 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet dispatcher threw exception
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
at $Proxy15.flush(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy15.flush(Unknown Source)
at com.orphanware.web.dao.SampleDao.saveSample(SampleDao.java:22)
at com.orphanware.web.services.SampleService.saveSample(SampleService.java:23)
at com.orphanware.web.controller.HomeController.index(HomeController.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.bufferAndPostProcess(ContentBufferingFilter.java:169)
at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.doFilter(ContentBufferingFilter.java:126)
at org.sitemesh.config.ConfigurableSiteMeshFilter.doFilter(ConfigurableSiteMeshFilter.java:157)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- entity manager factory -->
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<!-- jpaVendorAdapter (works in conjunction with the persistence.xml) -->
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="HSQL" />
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<tx:annotation-driven
transaction-manager="transactionManager"
proxy-target-class="false" />
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
</beans>
以下是存储库代码:
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.orphanware.web.domain.SampleEntity;
@Repository
@Transactional
public class SampleDao {
@PersistenceContext(unitName="appPU")
private EntityManager em;
public void saveSample(SampleEntity sample) {
em.persist(sample);
em.flush();
}
public SampleEntity getSample(long id) {
return em.find(SampleEntity.class, id);
}
}
以下是调用此存储库的服务:
package com.orphanware.web.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.orphanware.web.dao.SampleDao;
import com.orphanware.web.domain.SampleEntity;
@Service
public class SampleService {
@Autowired
private SampleDao sampleDao;
public void saveSample(String name){
SampleEntity se = new SampleEntity();
se.setName(name);
sampleDao.saveSample(se);
}
public SampleEntity getSample(long id) {
return sampleDao.getSample(id);
}
}
和调用服务的控制器:
package com.orphanware.web.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.orphanware.web.domain.SampleEntity;
import com.orphanware.web.services.SampleService;
@Controller
public class HomeController {
@Autowired
private SampleService sampleService;
@RequestMapping({"/home", "/"})
public String index(Map<String, Object> model) {
String[] stuff ={"stuff0", "stuff1", "stuff3"};
for(String item : stuff) {
sampleService.saveSample(item);
}
SampleEntity firstOne = sampleService.getSample(0);
model.put("myVar", firstOne.name);
return "home/index";
}
}
最后是dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<context:component-scan base-package="com.orphanware.web" />
<context:annotation-config />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
如果我不调用flush,那么我没有得到异常,但没有任何内容持续存在。我也试过在方法上加上@Transactional注释,也改变了传播等等。
任何帮助都会受到极大的关注。也许我刚刚看了太长时间
答案 0 :(得分:1)
您似乎忘了为您的软件包启用组件扫描,尝试将以下行添加到您的配置中。
<context:component-scan base-package="com.orphanware" />
如果您简化了示例并在实际代码自我调用中使用,则可能是一个问题: (来自参考文献)
在代理模式(默认设置)下,只会拦截通过代理进入的“外部”方法调用。这意味着'自调用',即目标对象中调用目标对象的其他方法的方法,即使被调用的方法用@Transactional标记,也不会在运行时导致实际的事务!
答案 1 :(得分:1)
好的,我发现我是个白痴(总是如此)。这些行:
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<context:component-scan base-package="com.orphanware.web" />
<context:annotation-config />
不属于dispatcher-servlet.xml。堆垛机部分正确。这些行属于使用它们的上下文。在这种情况下,在applicationContext.xml中。 (我实际上对资源不确定,但其余的我是)
答案 2 :(得分:0)
我清楚地看到你的问题,因为我确实有同样的问题,我会告诉你原因。您只需要<context:component-scan base-package="com.orphanware.web" />
来扫描位于servlet上下文的@Controller bean,另一方面您可以使用@Service和@Repository bean在applicationContext.xml中进行扫描,但是您只需要&#39; t用扫描标签提到它。这就是为什么你只需要在applicationContext.xml中添加以下标记:
<context:component-scan base-package="com.orphanware.web.services" />
和
<context:component-scan base-package="com.orphanware.web.repositories" />
扫描包裹。
这就是我的朋友!
祝你好运!