使用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());
}
} 从数据库取数正常,但是保存不上。也没有报错。。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
<propertyname="defaultAutoCommit"value="false"/>
这句决定是否提交事务,你把它设为false,就是说明事务没有提交
事务不提交,数据是看不到数据的
要想数据库,看到数据就改为true
<propertyname="defaultAutoCommit"value="false"/>
这句决定是否提交事务,你把它设为false,就是说明事务没有提交
事务不提交,数据是看不到数据的
<propertyname="defaultAutoCommit"value="false"/>
这句决定是否提交事务,你把它设为false,就是说明事务没有提交
事务不提交,数据是看不到数据的
<propertyname="defaultAutoCommit"value="false"/>
这句决定是否提交事务,你把它设为false,就是说明事务没有提交
事务不提交,数据是看不到数据的
<propertyname="defaultAutoCommit"value="false"/>
这句决定是否提交事务,你把它设为false,就是说明事务没有提交
事务不提交,数据是看不到数据的
<propertyname="defaultAutoCommit"value="false"/>
这句决定是否提交事务,你把它设为false,就是说明事务没有提交
事务不提交,数据是看不到数据的
<propertyname="defaultAutoCommit"value="false"/>
这句决定是否提交事务,你把它设为false,就是说明事务没有提交
事务不提交,数据是看不到数据的
<propertyname="defaultAutoCommit"value="false"/>
这句决定是否提交事务,你把它设为false,就是说明事务没有提交
事务不提交,数据是看不到数据的
<propertyname="defaultAutoCommit"value="false"/>
这句决定是否提交事务,你把它设为false,就是说明事务没有提交
事务不提交,数据是看不到数据的
在测试类上面加上这个注解就OK了@TransactionConfiguration(defaultRollback=false)