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

问题描述

环境:spring 3.1hibernate 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实例 --><beanclass="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /><!-- 对标注@Repository的类经行增强,将EntityManager异常转换为SpringDAO体系的异常 --><beanclass="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;}@Overridepublic 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;@Repositorypublic class IdentityDaoImpl implements IIdentityDao {@PersistenceContextprivate EntityManager em;@Transactional(propagation = Propagation.REQUIRED)public Identity create(Identity data) {em.persist(data);return data;}@Overridepublic Identity retriver(String id) throws ServicePlatformException {Long pk = Long.valueOf(id);return em.find(Identity.class, pk);}@Overridepublic Identity update(Identity data) throws ServicePlatformException {em.persist(data);return data;}@Overridepublic 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);@Resourceprivate IIdentityDao dao;@RequestMapping("retriver")@ResponseBodypublic Identity retriver(@RequestParam String id)throws ServicePlatformException {return dao.retriver(id);}@RequestMapping("create")@ResponseBodypublic 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")@ResponseBodypublic 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

解决方案

说明:<context:component-scan base-package="com.lxl" /> 这个扫描组件默认会扫描@Component,@Service,@Repository,@Controller注解,也就是说,你的业务层和持久层的bean被扫描了两次,因为你两个xml文件中都有这个配置。而Controller层的bean要比业务层和持久层的bean晚加载,所以会覆盖它们,而你在控制层中又没有事务配置(也就是dispatcher-servlet.xml文件中没有事务控制),所以可能导致事务不起作用。如果是上面这个问题,那么:dispatcher-servlet.xml这么配置,只扫描@Controller注解:<!-- 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 base-package="com.lxl"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /></context:component-scan>
解决方案二:
你的jpa没有配置扫描javabean 的路径啊?<property name="scanPackages" value="com.inspur.domain" />个人签名:健康宝宝
解决方案三:
You config has some issues. You have 2 component-scanS in applicationContext.xml and dispatcher-servlet.xml. Check some material to see what the difference is in these 2 xmlS.

时间: 2024-12-09 11:07:06

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

Spring集成JSF的最简单方式

Spring集成JSF的最简单方式是使用DelegatingVariableResolver类.在faces-config.xml中配置,示例如下: <application> <variable-resolver>org.springframework.web.jsf. DelegatingVariableResolver</variable-resolver> <locale-config> <default-locale>zh_CN<

spring-框架Spring和Hibernate 无法进行测试数据库的数据注入

问题描述 框架Spring和Hibernate 无法进行测试数据库的数据注入 解决方案 很奇葩的问题!报空指针异常,目测配置文件没有错误! 解决方案二: 很奇葩的问题!报空指针异常,目测配置文件没有错误! 解决方案三: 先看看passengerDaoTest.java的第15行是什么 解决方案四: 第十五行是:ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); 解决方案五

Spring与Hibernate两种组合方式

Spring与Hibernate大致有两种组合方式,主要区别是一种是在Hibernate中的hibernate.cfg.xml中配置数据源,一种是借助Spring的jdbc方式在Spring的applicationContext.xml文件中配置数据源,然后在Spring配置sessionFactory的bean有些区别 下面大致的说明一下 第一种 1.hibernate.cfg.xml文件 xml version='1.0' encoding='utf-8'?>          "-/

急,hibernate annotation JPA注解下的单向OneToOne问题

问题描述 2张表A,BA表中有一外键指向B表的主键,用@OneToOne(targetEntity = B.class,fetch = FetchType.LAZY)注解后,利用hibernate自动生成的表中,B表中也对应出现了A表的外键字段,我现在希望A表中有B的外键但B中不能有A的外键,请问该怎么设置?A:id, name,B_idB:id,name 解决方案 @OneToOne(targetEntity = B.class,fetch = FetchType.LAZY) 是否在B类中也设

spring mvc整合freemarker基于注解方式_实用技巧

基于网络改进为:最正常版本 复制代码 代码如下: <?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:p="htt

spring集成hibernate 报错 The application must supply JDBC connections

问题描述 Caused by: java.lang.UnsupportedOperationException: The application must supply JDBC connectionshibernate和spring的配置如图请帮我分析分析 解决方案 mysql驱动包有没有引入 ?

一个简单的hibernate与spring集成

下面介绍一下hibernate与spring的集成,这两个框架的集成关键在于 applicationContext.xml文件的配置,其实很简单的,但需要注意的是,导入包 时一定选择导入到webroot/web-inf/lib目录中,而且hibernate一般要在spring 之前导入,因为集成时要用到sessionFactory类, 一.初始化工作:新建一个项目,如(ssh),在此项目中导入对hibernate的支持 ,新建一个表sstest表,有字段id,username,password,

sping hibernate 事务-spring注解方式事务控制没有回滚

问题描述 spring注解方式事务控制没有回滚 项目中使用到了hibernate以及spring事务控制,在service层增加事务控制但是遇到异常没有回滚. 代码如下: 配置文件 <?xml version="1.0" encoding="UTF-8"?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springfr

spring集成jpa【为什么有 persistant.xml 文件呢?】

原文地址: http://www.cnblogs.com/javahuang/archive/2012/12/19/2824633.html spring集成JPA的其中一种方式 JPA和hibernate的关系: spring对JPA的集成.JPA是一个ORM规范,由厂商实现该规范, 目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现,本系统中用到的就是hibernate的实现JPA,只配置了两个属性datasource和实现厂商) 所谓JPA是持久化的规范,