开发者社区> 问答> 正文

使用spring-data-jpa保存数据,提交成功,但是数据库没有保存到数据,?报错

使用spring-data-jpa,用的是hibernate的Jpa实现,测试保存没有报错,但是数据库没有存上,原因应该是事务最终没有提交,感觉上是这个样子的。但是 不知道怎么配置。请解答。
数据库:postgresql 8.4

spring配置文件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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:repository="http://www.springframework.org/schema/data/repository"
	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/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
       http://www.springframework.org/schema/data/repository
       http://www.springframework.org/schema/data/repository/spring-repository-1.0.xsd">

	<aop:aspectj-autoproxy proxy-target-class="true"/>

	<context:component-scan base-package="test">
		<context:include-filter type="annotation"
			expression="org.aspectj.lang.annotation.Aspect" />
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<jpa:repositories base-package="test.repository"
		entity-manager-factory-ref="entityManagerFactory"
		transaction-manager-ref="transactionManager" />

	<context:annotation-config />

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

	<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClass}" />
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="defaultAutoCommit" value="false" />
	</bean>

	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
		<property name="packagesToScan" value="com.palmer.permission.domain"/>
		<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
	</bean>

	<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
	    <property name="databasePlatform" value="${hibernate.dialect}" />  
	</bean> 

	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

</beans>
JPA配置文件 persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> 
 
 	<persistence-unit name="palmer.unit" transaction-type="RESOURCE_LOCAL"> 
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>test.domain.User</class>
        <class>test.domain.Role</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        
        <properties>
            <property name="generateDdl" value="true" />
            <property name="databasePlatform" value="${hibernate.dialect}" />
            <property name="hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"/>
            <property name="hibernate.show_sql" value="${hibernate.show_sql}"/>
            <property name="hibernate.format_sql" value="${hibernate.format_sql}"/>
            <!-- <property name="hibernate.connection.isolation" value="8"/> -->
            <property name="hibernate.current_session_context_class" value="thread"/>
        </properties>
    </persistence-unit>
</persistence>
UserRepository接口类:
public interface UserRepository extends PagingAndSortingRepository<User, Long> {

	User findByNameAndPassword(String name,String password);
}
UserService类:
@Service
@Transactional
public class UserService {

	
	@Autowired
	private UserRepository userRepo;
	
	
	public User save(User user){
		return userRepo.save(user);
	}
	
	
	public Page<User> getUsers(Integer pageNumber){
		PageRequest pageRequest = new PageRequest(pageNumber - 1, Context.PAGE_SIZE, Sort.Direction.DESC,"id");
		return userRepo.findAll(pageRequest);
	}
	
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-core.xml")
@Transactional
public class RepositoryTest {

	@Autowired
	UserService userService;
	

	@Test
	public void getByService(){
		Page<User> page = userService.getUsers(1);
		List<User> list = page.getContent();
		for(User u:list){
			System.out.println(u.getDisplayName()+",isDisable:"+u.isDisable());
		}
	}
	
	@Test
	public void saveUser(){
		User u = new User();
		u.setName("test").setPassword("123").setDisplayName("测试用户");
		User u2 = userService.save(u);
		System.out.println(u2.getId());
	}
	
	
}
从数据库取数正常,但是保存不上。也没有报错。。

展开
收起
爱吃鱼的程序员 2020-06-22 21:14:14 4170 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    <propertyname="defaultAutoCommit"value="false"/>

    这句决定是否提交事务,你把它设为false,就是说明事务没有提交

    事务不提交,数据是看不到数据的

    要想数据库,看到数据就改为true

    引用来自“tx15”的答案

    <propertyname="defaultAutoCommit"value="false"/>

    这句决定是否提交事务,你把它设为false,就是说明事务没有提交

    事务不提交,数据是看不到数据的

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    <propertyname="defaultAutoCommit"value="false"/>

    这句决定是否提交事务,你把它设为false,就是说明事务没有提交

    事务不提交,数据是看不到数据的

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    <propertyname="defaultAutoCommit"value="false"/>

    这句决定是否提交事务,你把它设为false,就是说明事务没有提交

    事务不提交,数据是看不到数据的

    引用来自“itwarcraft”的答案

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    <propertyname="defaultAutoCommit"value="false"/>

    这句决定是否提交事务,你把它设为false,就是说明事务没有提交

    事务不提交,数据是看不到数据的

    引用来自“tx15”的答案

    引用来自“itwarcraft”的答案

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    <propertyname="defaultAutoCommit"value="false"/>

    这句决定是否提交事务,你把它设为false,就是说明事务没有提交

    事务不提交,数据是看不到数据的

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    引用来自“itwarcraft”的答案

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    <propertyname="defaultAutoCommit"value="false"/>

    这句决定是否提交事务,你把它设为false,就是说明事务没有提交

    事务不提交,数据是看不到数据的

    引用来自“itwarcraft”的答案

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    引用来自“itwarcraft”的答案

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    <propertyname="defaultAutoCommit"value="false"/>

    这句决定是否提交事务,你把它设为false,就是说明事务没有提交

    事务不提交,数据是看不到数据的

    引用来自“皛尛惢”的评论

    引用来自“itwarcraft”的答案

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    引用来自“itwarcraft”的答案

    引用来自“itwarcraft”的答案

    引用来自“tx15”的答案

    <propertyname="defaultAutoCommit"value="false"/>

    这句决定是否提交事务,你把它设为false,就是说明事务没有提交

    事务不提交,数据是看不到数据的

    在测试类上面加上这个注解就OK了@TransactionConfiguration(defaultRollback=false)
    2020-06-22 21:14:30
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载