我在春季启动应用程序中使用spring数据jpa。
以下是我按学校ID删除学校的SchoolService方法。
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public boolean deleteSchool(Long id) {
log.debug("Deleting school entry with the school id: {}", id);
try {
schoolRepository.delete(id);
return true;
} catch(Exception e){
log.error(e.toString());
return false;
}
}
和我的SchoolRepository如下。
public interface SchoolRepository<School, Long> extends PagingAndSortingRepository<School, Long> {
}
当删除学校时出现异常时,此服务方法应该抛出一个应该被捕获的异常,并且应该返回false,表明删除操作已经失败。
但是在上面的例子中,它返回true然后抛出异常。不确定为什么?
下面是stacktrace
2016-07-27 16:13:30.809 DEBUG 7650 --- [nio-8080-exec-6] c.sts.app.core.common.aop.LoggingAspect : Enter: com.sts.app.school.service.SchoolService.deleteSchool() with argument[s] = [1]
2016-07-27 16:13:30.809 DEBUG 7650 --- [nio-8080-exec-6] c.s.a.s.service.impl.SchoolServiceImpl : Deleting school entry with the school id: 1
Hibernate:
select
school0_.id as id1_4_0_,
school0_.created_by as created_2_4_0_,
school0_.created_date as created_3_4_0_,
school0_.last_modified_by as last_mod4_4_0_,
school0_.last_modified_date as last_mod5_4_0_,
school0_.address as address6_4_0_,
school0_.name as name7_4_0_
from
schools school0_
where
school0_.id=?
2016-07-27 16:13:30.812 DEBUG 7650 --- [nio-8080-exec-6] c.sts.app.core.common.aop.LoggingAspect : Exit: com.sts.app.school.service.SchoolService.deleteSchool() with result = true
Hibernate:
delete
from
schools
where
id=?
2016-07-27 16:13:30.815 WARN 7650 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1451, SQLState: 23000
2016-07-27 16:13:30.815 ERROR 7650 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : Cannot delete or update a parent row: a foreign key constraint fails (`blank_app`.`students`, CONSTRAINT `FK_d8qlrs5j411ofxq8uy2nlhxm` FOREIGN KEY (`school_id`) REFERENCES `schools` (`id`))
2016-07-27 16:13:30.816 WARN 7650 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 1451, SQLState: 23000
2016-07-27 16:13:30.816 WARN 7650 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : Cannot delete or update a parent row: a foreign key constraint fails (`blank_app`.`students`, CONSTRAINT `FK_d8qlrs5j411ofxq8uy2nlhxm` FOREIGN KEY (`school_id`) REFERENCES `schools` (`id`))
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:259)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy126.deleteSchool(Unknown Source)
at com.sts.app.school.web.SchoolController.deleteSchool(SchoolController.java:71)
at com.sts.app.school.web.SchoolController$$FastClassByCGLIB$$a5f9e3ee.invoke(<generated>)