的 所以每表的hibernate序列得到更新,但我的实体没有插入到数据库中。 强>
即使没有两个相同的Spring上下文的问题,我也遇到了相同的症状。 forceAjcCompile选项解决了问题:
... <java.version>1.6</java.version> <aspectj.version>1.7.0</aspectj.version> ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.6</version> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>compile</goal> </goals> </execution> </executions> <configuration> <outxml>true</outxml> <showWeaveInfo>false</showWeaveInfo> <verbose>false</verbose> <complianceLevel>${java.version}</complianceLevel> <forceAjcCompile>true</forceAjcCompile> <aspectLibraries> <aspectLibrary> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </aspectLibrary> </aspectLibraries> <source>${java.version}</source> <target>${java.version}</target> <Xlint>ignore</Xlint> </configuration> </plugin>
我在这个链接后面找到了答案: http://forum.springsource.org/showthread.php?18953-DispatcherServlet-and-ContextLoaderListener 。
问题是由于我也在使用Spring MVC并且我在不知道两个几乎相同的Spring上下文的情况下创建的。因此,事务管理器在第一个上下文(接收JAX-WS调用的那个)中管理事务,但我调用的实体管理器由第二个上下文(使用不同的事务管理器)管理。
解决方案是隔离DispatcherServlet的小的简化上下文定义,并让其余的bean由ContextLoaderListener管理:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/application-context.xml</param-value> </context-param> <servlet> <servlet-name>spring-mvc-dispatcher-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/servlet-context.xml</param-value> </init-param> </servlet>
由于我使用了基于注释的MVC(带有@Controller注释),我还必须减少上下文的范围:component-scan的“servlet”上下文中的base-package。
以下是保存我一天的链接的引用:
DispatcherServlet将始终使用-servlet.xml加载自己的配置文件。此文件旨在包含Web组件,如Controllers,ViewResolvers和LocaleResolvers - 但不包含中间层组件。 然后使用ContextLoaderListener加载包含中间层和数据层组件的文件。 Spring会将所有这些组件合并到一个ApplicationContext中,使您的中间层组件可以从Web层组件中访问。 &gt; Rob Harrop首席工程师,dm Server
DispatcherServlet将始终使用-servlet.xml加载自己的配置文件。此文件旨在包含Web组件,如Controllers,ViewResolvers和LocaleResolvers - 但不包含中间层组件。
然后使用ContextLoaderListener加载包含中间层和数据层组件的文件。 Spring会将所有这些组件合并到一个ApplicationContext中,使您的中间层组件可以从Web层组件中访问。 &gt; Rob Harrop首席工程师,dm Server
如果您使用EntityManager,则不必处理代码中的任何休眠问题。 这正是jpa的用途。
前段时间我遇到了同样的问题。
您的EntityManager必须注入@PersistenceContext。 getEntityManager()不会这样做。
而且您的注释功能必须是公开的。