Could not commit JPA transaction, Transaction marked as rollbackOnly

无敌大金金 发布于 2013/07/30 11:28
阅读 9K+
收藏 1
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
	at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:522)
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:591)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:297)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:192)
	at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:395)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:91)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
	at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:72)
	at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:513)
	... 25 more

错误如上

实体类

@Entity
@Table(name = "sys_address")
@DynamicInsert @DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Address implements Serializable{

        private static final long serialVersionUID = 1L;
	private Long	id;					// 编号
	private Long  	userId; 			// 所属用户id
	private String  consignee; 			// 收件人名

        @Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
....get set......
}

DAO: 其中BASE的没什么特殊的额常用DAO

public interface AddressDao extends AddressDaoCustom, CrudRepository<Address, Long> {

	@Modifying
	@Query("update Address set delFlag=?2 where id = ?1")
	public int updateDelFlag(Long id, String status);
	
}

interface AddressDaoCustom extends BaseDao<Address> {

}

@Repository
class AddressDaoImpl extends BaseDaoImpl<Address> implements AddressDaoCustom {

}
Service:

@Service
@Transactional(readOnly = true)
public class AddressService{
        @Autowired
	private AddressDao addressDao;

	@Transactional(readOnly = false)
	public void saveAddress(Address address){
		addressDao.save(address);
	}
}
Spring 配置

<!-- 加载配置属性文件 -->
	<context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" />
	

	<bean id="springContextHolder" class="com.cargo.common.utils.SpringContextHolder" lazy-init="false"/>
	
	<context:component-scan base-package="com.cargo">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
		<property name="dataSource" ref="dataSource"/>
		<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/> 
		<property name="packagesToScan" value="com.cargo"/>
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
				<prop key="net.sf.ehcache.configurationResourceName">cache/ehcache-hibernate-local.xml</prop>
				<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
				<prop key="hibernate.search.default.directory_provider">filesystem</prop>
				<prop key="hibernate.search.default.indexBase">${hibernate.search.default.indexBase}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
			</props>
		</property>
	</bean>
	
	<!-- 指定JPA实现厂商适配器 -->
	<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
	
	<!-- 配置 Spring Data JPA, base-package 如果多个,用“,”分隔 -->
 	<jpa:repositories base-package="com.cargo"  transaction-manager-ref="transactionManager"
 		entity-manager-factory-ref="entityManagerFactory"/>
   
	<!-- 配置 JPA Transaction -->
	<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"/>
	

	<!-- 配置 JSR303 Bean Validator 定义 -->
	<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

加载中
0
无敌大金金
无敌大金金

查到原因了~ 因为一个字段长度超长 但是没有报错~ 很奇怪~

@Length(min=0, max=10)

Hibernate Validator 5.0.1 Final

居然没有提示- -!


返回顶部
顶部