jpa spring 事务配置 无法保存数据 事务提交不了。

这几天经理搭了个框架,但是事务出了点问题,架子用的是jpa2+spring 3 mvc ,但是配置完成后只能查询不能保存数据。

 

代码采用的是maven管理的。  工程分parent,util,web-util ,domain,app 等应用来完成。

 

这样有利于代码的分离管理。

 

 

app的pom.xml 为:(主要是说明用了哪些依赖jar包)

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

 <parent>
  <artifactId>com.lsoba.parent</artifactId>
  <groupId>com.lsoba</groupId>
  <version>0.0.1-SNAPSHOT</version>
  <relativePath>../parent</relativePath>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.lsoba.main</groupId>
 <artifactId>com.lsoba.main</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>

 <name>com.lsoba.main</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <pluginRepositories>
  <pluginRepository>
   <id>spring-maven-release</id>
   <name>Spring Maven Release Repository</name>
   <url>http://maven.springframework.org/release</url>
  </pluginRepository>
  <pluginRepository>
   <id>spring-maven-milestone</id>
   <name>Spring Maven Milestone Repository</name>
   <url>http://maven.springframework.org/milestone</url>
  </pluginRepository>
  <pluginRepository>
   <id>spring-roo-repository</id>
   <name>Spring Roo Repository</name>
   <url>http://spring-roo-repository.springsource.org/release</url>
  </pluginRepository>
 </pluginRepositories>

 <dependencies>
  <dependency>
   <groupId>com.lsoba.domain</groupId>
   <artifactId>com.lsoba.domain</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
  <dependency>
   <groupId>com.lsoba.util</groupId>
   <artifactId>com.lsoba.util</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>
  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>jcl-over-slf4j</artifactId>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
  </dependency>
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
  </dependency>
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>net.sf.flexjson</groupId>
   <artifactId>flexjson</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aspects</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
  </dependency>
  <dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
  </dependency>
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
  </dependency>
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
  </dependency>
  <dependency>
   <groupId>org.hibernate.javax.persistence</groupId>
   <artifactId>hibernate-jpa-2.0-api</artifactId>
  </dependency>
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.validation</groupId>
   <artifactId>validation-api</artifactId>
  </dependency>
  <dependency>
   <groupId>cglib</groupId>
   <artifactId>cglib-nodep</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.transaction</groupId>
   <artifactId>jta</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-orm</artifactId>
  </dependency>
  <dependency>
   <groupId>commons-pool</groupId>
   <artifactId>commons-pool</artifactId>
  </dependency>
  <dependency>
   <groupId>commons-dbcp</groupId>
   <artifactId>commons-dbcp</artifactId>
  </dependency>
  <dependency>
   <groupId>org.apache.tiles</groupId>
   <artifactId>tiles-core</artifactId>
  </dependency>
  <dependency>
   <groupId>org.apache.tiles</groupId>
   <artifactId>tiles-jsp</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.webflow</groupId>
   <artifactId>spring-js-resources</artifactId>
  </dependency>
  <dependency>
   <groupId>commons-digester</groupId>
   <artifactId>commons-digester</artifactId>
  </dependency>
  <dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.el</groupId>
   <artifactId>el-api</artifactId>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>joda-time</groupId>
   <artifactId>joda-time</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-core</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-config</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-taglibs</artifactId>
  </dependency>
 </dependencies>
 <build>
  <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
  <outputDirectory>${basedir}/Web/WEB-INF/classes</outputDirectory>
  <resources>
   <resource>
    <directory>${basedir}/src/main/java</directory>
    <excludes>
     <exclude>**/*.java</exclude>
    </excludes>
   </resource>
  </resources>
  <plugins>
   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
     <webappDirectory>${basedir}/Web</webappDirectory>
     <warSourceDirectory>${basedir}/Web</warSourceDirectory>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

 

 

web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>lsoba</display-name>
 <context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>lsoba.root</param-value>
 </context-param>
 <context-param>
  <param-name>defaultHtmlEscape</param-name>
  <param-value>true</param-value>
 </context-param>
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:META-INF/log4j.properties</param-value>
 </context-param>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
 </context-param>
 <filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 </filter>
 <filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>
 <filter>
  <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
  <filter-class>
   org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <listener>
  <listener-class>
   org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
 </listener>
 <listener>
  <listener-class>
   org.springframework.web.context.request.RequestContextListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>appServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:META-INF/spring/controllers-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>appServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 <error-page>
  <exception-type>java.lang.NullPointerException</exception-type>
  <location>/error/error.jsp</location>
 </error-page>
 <error-page>
  <error-code>404</error-code>
  <location>/error/404.jsp</location>
 </error-page>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 
</web-app>

 

 spring 的配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
 <context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
 
 <context:spring-configured />
 
 <context:component-scan base-package="com.lsoba" />

 <context:property-override location="classpath:override.properties" />

 <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:3306/test" />
  <property name="username" value="test" />
  <property name="password" value="test" />
 </bean>


 <bean class="org.springframework.orm.jpa.JpaTransactionManager"
  id="transactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>
 <tx:annotation-driven proxy-target-class="true"
  transaction-manager="transactionManager" />
 <bean
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
  id="entityManagerFactory">
  <property name="dataSource" ref="dataSource" />
 </bean>
</beans>

 

其它配置controllers-servlet.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <!--
  Resolves views selected for rendering by @Controllers to .jsp
  resources in the /WEB-INF/views directory
 -->
 <beans:bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <beans:property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <beans:property name="prefix" value="/WEB-INF/jsp/" />
  <beans:property name="suffix" value=".jsp" />
 </beans:bean>

 <!--
  Imports user-defined @Controller beans that process client requests
 -->
 <beans:import resource="controllers.xml" />

 <!--
  Only needed because we require fileupload in the
  org.springframework.samples.mvc.fileupload package
 -->
 <beans:bean id="multipartResolver"
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
  
</beans:beans>

 

controllers.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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <!-- The controllers are autodetected POJOs labeled with the @Controller annotation. -->
 <context:component-scan base-package="com.lsoba" use-default-filters="false">
  <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
 </context:component-scan>

 <!-- 必须要加 -->
 <mvc:annotation-driven />
 
 <mvc:default-servlet-handler/>

 <mvc:resources location="/resources/" mapping="/resources/**" />
 <mvc:resources location="/ckeditor/" mapping="/ckeditor/**" />
 <mvc:resources location="/error/" mapping="/error/**" />
 <mvc:resources location="/favicon.ico" mapping="/favicon*" />

</beans>

 

主要原因就出在:

jee的支持上:

得在.classpath上加上

<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER">
  <attributes>
   <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
  </attributes>
 </classpathentry>
 <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
 <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>

 

这样就ok了。事务也可以保存了。

 

 

哈哈。应该可以了,可以参考我的项目:http://www.lsoba.com

也可以去论坛留言。如果碰到问题可以去:http://bbs.lsoba.cn发问。有时间就可以一起学习讨论哈。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值