leeray3017 2013-12-18 01:03
浏览 455
已采纳

Spring集成Hibernate,使用JPA注解方式,新增数据无法提交

环境:

spring 3.1

hibernate 4.2.8 final 

mysql 5.6

问题描述:

调用controller的查询方法,可以正常返回数据

调用controller的创建方法,即:新增数据,控制台打印出了读取和修改序号表的sql,但是没有打印插入数据的sql,数据库表中也没有新增数据。

怀疑是spring控制的事务没有提交,但是排查了两三天,从网上找了各种方法,都没有解决问题。

请各位大侠拉小弟一把,实在是找不到事务为什么没有提交。

 

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">

    <!-- 欢迎界面 -->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <!-- spring框架 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 上下文配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml,
            /WEB-INF/config/**/*.xml
        </param-value>
    </context-param>


    <!-- spring mvc -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <!-- 拦截所有url -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 回话有效期30分钟 -->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <!-- log4j日志 -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!-- log4j日志配置 -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/properties/log4j.properties</param-value>
    </context-param>



</web-app>

 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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 注解配置 -->
    <context:annotation-config />

    <!--允许使用注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 自动扫描的包名 -->
    <context:component-scan base-package="com.lxl" />

    <!-- 对标注@PersistenceContext的类经行增强,引入代理后的EntityManager实例 -->
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <!-- 对标注@Repository的类经行增强,将EntityManager异常转换为SpringDAO体系的异常 -->
    <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <!-- 属性文件 -->
    <context:property-placeholder location="/WEB-INF/properties/**/*.properties" />

    <!-- 数据源 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>


    <!-- JPA 实体管理工厂 -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="ServicePlatform" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
            </bean>
        </property>
    </bean>

    <!-- JPA 事务管理 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="dataSource" ref="dataSource" />
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    

</beans>

 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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   
    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven />

    <!-- 自动扫描的包名 -->
    <context:component-scan base-package="com.lxl" />


    <!-- 配置一下对json数据的转换 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
        <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> 
        </list> </property> </bean> -->

    <!-- 视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 对静态资源文件的访问 方案一 (二选一) -->
    <mvc:default-servlet-handler />

    <!-- 对静态资源文件的访问 方案二 (二选一) <mvc:resources mapping="/images/**" location="/images/" 
        cache-period="31556926"/> <mvc:resources mapping="/js/**" location="/js/" 
        cache-period="31556926"/> <mvc:resources mapping="/css/**" location="/css/" 
        cache-period="31556926"/> -->
        

</beans>

 

persistence.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="ServicePlatform"
        transaction-type="RESOURCE_LOCAL">
    </persistence-unit>
</persistence>

 

实体bean

 

package com.lxl.bsp.identity.entity;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "bd_id")
public class Identity implements Serializable {
    private static final long serialVersionUID = -8763611811485844772L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    @Column(length = 100)
    private String code;

    private Long value;

    @Column(length = 10)
    private String prefix;

    @Column(length = 10)
    private String suffix;

    @Column(length = 255)
    private String description;

    private Integer length;

    @Column(length = 30)
    private String creator;

    @Column(name = "create_date")
    private Timestamp createDate;

    @Column(length = 30)
    private String modifier;

    @Column(name = "modify_Date")
    private Timestamp modifyDate;

    private Boolean dr;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Long getValue() {
        return value;
    }

    public void setValue(Long value) {
        this.value = value;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    public String getModifier() {
        return modifier;
    }

    public void setModifier(String modifier) {
        this.modifier = modifier;
    }

    public Timestamp getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Timestamp modifyDate) {
        this.modifyDate = modifyDate;
    }

    public Boolean getDr() {
        return dr;
    }

    public void setDr(Boolean dr) {
        this.dr = dr;
    }

    @Override
    public String toString() {
        return "Identity [id=" + id + ", code=" + code + ", value=" + value
                + ", prefix=" + prefix + ", suffix=" + suffix
                + ", description=" + description + ", length=" + length
                + ", creator=" + creator + ", createDate=" + createDate
                + ", modifier=" + modifier + ", modifyDate=" + modifyDate
                + ", dr=" + dr + "]";
    }

}

 

IdentityDaoImpl.java

 

package com.lxl.bsp.identity.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lxl.bsp.api.exception.ServicePlatformException;
import com.lxl.bsp.identity.entity.Identity;

@Repository
public class IdentityDaoImpl implements IIdentityDao {

    @PersistenceContext
    private EntityManager em;

    @Transactional(propagation = Propagation.REQUIRED)
    public Identity create(Identity data) {
        em.persist(data);
        return data;
    }

    @Override
    public Identity retriver(String id) throws ServicePlatformException {
        Long pk = Long.valueOf(id);
        return em.find(Identity.class, pk);
    }

    @Override
    public Identity update(Identity data) throws ServicePlatformException {
        em.persist(data);
        return data;
    }

    @Override
    public void delete(String id) throws ServicePlatformException {
        em.remove(retriver(id));
    }
}

 IdentityController.java

 

 

package com.lxl.bsp.identity.controller;

import java.sql.Timestamp;
import java.util.Calendar;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lxl.bsp.api.exception.ServicePlatformException;
import com.lxl.bsp.identity.dao.IIdentityDao;
import com.lxl.bsp.identity.entity.Identity;

@Controller
@RequestMapping("identity")
public class IdentityController {

    private static Log log = LogFactory.getLog(IdentityController.class);

    @Resource
    private IIdentityDao dao;

    @RequestMapping("retriver")
    @ResponseBody
    public Identity retriver(@RequestParam String id)
            throws ServicePlatformException {
        return dao.retriver(id);
    }

    @RequestMapping("create")
    @ResponseBody
    public Identity create() throws ServicePlatformException {
        int i = 1;
        Identity data = new Identity();
        data.setCode("code" + i);
        Calendar c = Calendar.getInstance();
        data.setCreateDate(new Timestamp(c.getTime().getTime()));
        data.setCreator("creator" + i);
        data.setDescription("description" + i);
        data.setDr(false);
        // data.setId("id" + Integer.toString(i));
        data.setLength(30);
        data.setModifier("modifier" + i);
        data.setModifyDate(new Timestamp(c.getTime().getTime()));
        data.setPrefix("prefix");
        data.setSuffix("suffix");
        data.setValue(0L);
        Identity returnData = dao.create(data);
        log.debug(returnData);
        return returnData;
    }

    @RequestMapping("update")
    @ResponseBody
    public Identity update() throws ServicePlatformException {
        int i = 1;
        Identity data = new Identity();
        data.setCode("1");
        Calendar c = Calendar.getInstance();
        data.setCreateDate(new Timestamp(c.getTime().getTime()));
        data.setCreator("creator" + i);
        data.setDescription("description" + i);
        data.setDr(false);
        // data.setId("id" + Integer.toString(i));
        data.setLength(30);
        data.setModifier("modifier" + i);
        data.setModifyDate(new Timestamp(c.getTime().getTime()));
        data.setPrefix("prefix");
        data.setSuffix("suffix");
        data.setValue(0L);
        Identity returnData = dao.update(data);
        log.debug(returnData);
        return returnData;
    }

    @RequestMapping("delete")
    public void delete(@RequestParam String id) throws ServicePlatformException {
        dao.delete(id);
    }

}

 

数据表结构DDL

 

-- ----------------------------
-- Table structure for bd_id
-- ----------------------------
DROP TABLE IF EXISTS `bd_id`;
CREATE TABLE `bd_id` (
  `id` bigint(30) NOT NULL,
  `code` varchar(100) DEFAULT NULL,
  `create_date` datetime DEFAULT NULL,
  `creator` varchar(30) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  `dr` tinyint(1) DEFAULT NULL,
  `length` int(11) DEFAULT NULL,
  `modifier` varchar(30) DEFAULT NULL,
  `modify_Date` datetime DEFAULT NULL,
  `prefix` varchar(10) DEFAULT NULL,
  `suffix` varchar(10) DEFAULT NULL,
  `value` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

附件中是项目压缩包,只要电脑上安装了数据库,解压即可运行,数据库名称是osd

 

  • 写回答

3条回答 默认 最新

  • 三心不可得 2013-12-18 12:57
    关注

    说明:

    这个扫描组件默认会扫描@Component,@Service,@Repository,@Controller注解,也就是说,你的业务层和持久层的bean被扫描了两次,因为你两个xml文件中都有这个配置。而Controller层的bean要比业务层和持久层的bean晚加载,所以会覆盖它们,而你在控制层中又没有事务配置(也就是dispatcher-servlet.xml文件中没有事务控制),所以可能导致事务不起作用。

    如果是上面这个问题,那么:dispatcher-servlet.xml这么配置,只扫描@Controller注解:

    <context:component-scan base-package="com.lxl" use-default filters="false">
    <context:include-filter type="annotation"      expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    

    applicationController这么配置,不扫描@Controller主键:


    /context:component-scan

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问