我在springboot中有一个休息的Web服务。 Webservice正在删除照片,评论和地点。现在在调试时,REST服务会成功响应,但之后会显示MySQLIntegrityConstraintViolationException。现在,我知道它给出了错误,因为在其中添加了折扣。如果孩子存在或不存在,我需要显示一条消息。为此,我需要捕获此异常,但是我无法捕获该异常。我尝试了SQLException,DataIntegretyViolationException,但无法正常工作。
Web服务代码:
@GetMapping("deletePlaceById/{placeId}")
@Transactional
public RestResponse deletePlaceById(@PathVariable Long placeId) {
try {
List<PlacePhotosDto> photoList = placePhotosRepo.findByPlaceDto(new PlaceDto(placeId));
for (PlacePhotosDto placePhotosDto : photoList) {
AppUtilities.deleteFile(placePhotosDto.getPhotosUrl(), uploadFileDirectory);
}
List<PlacePhotosDto> imageList = placePhotosRepo.findByPlaceDto(new PlaceDto(placeId));
for (PlacePhotosDto placePhotosDto : imageList) {
placePhotosRepo.deleteById(placePhotosDto.getPhotosId());
}
List<PlaceReviewDto> reviewList = placeReviewRepo.findByPlaceDto(new PlaceDto(placeId));
for (PlaceReviewDto placeReviewDto : reviewList) {
placeReviewRepo.deleteById(placeReviewDto.getReviewId());
}
placeRepo.deleteById(placeId);
response = new RestResponse(AppConstants.RESPONSE_TEXT_SUCCESS, AppConstants.RESPONSE_CODE_SUCESS,
AppConstants.RESPONSE_TEXT_SUCCESS, true);
} catch (Exception e) {
e.printStackTrace();
response = new RestResponse(AppConstants.RESPONSE_TEXT_ERROR, AppConstants.RESPONSE_CODE_SERVER_ERROR,
AppConstants.RESPONSE_TEXT_ERROR, e.getMessage());
}
//This response shows success on debug in eclipse.
return response;
}
例外:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`questapp`.`cs_partner_discount`, CONSTRAINT `FK_dis_place` FOREIGN KEY (`place_id`) REFERENCES `cs_place` (`place_id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_60]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_60]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_60]
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_60]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-2.7.8.jar:na]
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-2.7.8.jar:na]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3325) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3562) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:99) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:599) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
and so on.