如何解决Spring boot 调用删除方法在某些情况下不起作用
我有这样的 CurrentTerminalStatus 实体:
@Entity
@Table(name = "current_terminal_status")
public class CurrentTerminalStatus extends DateAudit {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(notes = "The database generated CurrentTerminalStatus ID.")
private Long id;
@Column(name = "current_terminal_global_status_ok")
@NotNull(message = "currentTerminalGlobalStatusOK cannot be null.")
@ApiModelProperty(notes = "currentTerminalGlobalStatusOK for the CurrentTerminalStatus.",required = true)
private boolean currentTerminalGlobalStatusOK;
@OnetoMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = "currentTerminalStatus",orphanRemoval = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<CurrentTerminalErrors> errors;
@OnetoMany(cascade = CascadeType.ALL,orphanRemoval = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<CurrentTerminalWarnings> warnings;
........
getter/setter
}
CurrentTerminalErrors 是:
@Entity
@Table(name = "current_terminal_errors")
public class CurrentTerminalErrors {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(notes = "The database generated CurrentTerminalErrors ID.")
private Long id;
@NotNull(message = "errorNumber cannot be null.")
@ApiModelProperty(notes = "errorNumber is the number of the error.",required = true)
@Column(name = "error_number",nullable = false)
private Integer errorNumber;
@NotBlank(message = "errorName cannot be missing or empty.Please define errorName.")
@ApiModelProperty(notes = "errorName is the description of the error.",required = true)
@Column(name = "error_name",nullable = false)
private String errorName;
@NotBlank(message = "errorDescription cannot be missing or empty.Please define errorDescription.")
@ApiModelProperty(notes = "errorDescription is the description of the error.",required = true)
@Column(name = "error_description",nullable = false)
private String errorDescription;
@NotBlank(message = "errorActions cannot be missing or empty.Please define errorActions.")
@ApiModelProperty(notes = "errorActions is the actions for the error.",required = true)
@Column(name = "error_actions",nullable = false)
private String errorActions;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "current_terminal_status_id",nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private CurrentTerminalStatus currentTerminalStatus;
......
getter/setter
}
当前终端警告:
@Entity
@Table(name = "current_terminal_warnings")
public class CurrentTerminalWarnings {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(notes = "The database generated CurrentTerminalWarnings ID.")
private Long id;
@NotNull(message = "warningNumber cannot be null.")
@ApiModelProperty(notes = "warningNumber is the number of the warning.",required = true)
@Column(name = "warning_number",nullable = false)
private Integer warningNumber;
@NotBlank(message = "warningName cannot be missing or empty.Please define warningName.")
@ApiModelProperty(notes = "warningName is the name of the warning.",required = true)
@Column(name = "warning_name",nullable = false)
private String warningName;
@NotBlank(message = "warningDescription cannot be missing or empty.Please define warningDescription.")
@ApiModelProperty(notes = "warningDescription is the description of the warning.",required = true)
@Column(name = "warning_description",nullable = false)
private String warningDescription;
@NotBlank(message = "warningActions cannot be missing or empty.Please define warningActions.")
@ApiModelProperty(notes = "warningActions is the actions for the warning.",required = true)
@Column(name = "warning_actions",nullable = false)
private String warningActions;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY,nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private CurrentTerminalStatus currentTerminalStatus;
......
getter/setter
}
我的控制器:
public ResponseEntity<?> setCurrentTerminalStatus(final HttpServletRequest request,@NotNull @Valid @RequestBody final UpdateCurrentTerminalStatusRequest updateCurrentTerminalStatusRequest) {
final String token = request.getHeader("Authorization").split(" ")[1];
final User userFromAccesstoken = this.tokenProvider.getUserFromToken(token);
final CurrentTerminalStatus currentTerminalStatus = userFromAccesstoken.getCurrentTerminalStatus();
final Set<CurrentTerminalErrors> errors = currentTerminalStatus.getErrors(); //this is for testing
final Set<CurrentTerminalWarnings> warnings = currentTerminalStatus.getWarnings(); //this is for testing
if (currentTerminalStatus.isCurrentTerminalGlobalStatusOK() != updateCurrentTerminalStatusRequest.getCurrentTerminalGlobalStatusOK()){
sendNotification = currentTerminalStatus.isCurrentTerminalGlobalStatusOK() && !updateCurrentTerminalStatusRequest.getCurrentTerminalGlobalStatusOK();
}else{
sendNotification = compareTwoSets(warnings,warnings); //for testing
}
this.currentTerminalErroRSService.deleteCurrentTerminalErrorsByCurrentTerminalStatusId(currentTerminalStatus.getId()); //delete all terminal errors
this.currentTerminalWarningsService.deleteCurrentTerminalWarningsByCurrentTerminalStatusId(currentTerminalStatus.getId()); //delete all terminal warnings
currentTerminalStatus.setCurrentTerminalGlobalStatusOK(updateCurrentTerminalStatusRequest.getCurrentTerminalGlobalStatusOK());
final Set<CurrentTerminalErrors> errorsTerminal = new HashSet<>();
final Set<CurrentTerminalWarnings> warningsTerminal = new HashSet<>();
for (final CurrentTerminalErrors deviceErrors : updateCurrentTerminalStatusRequest.getErrors()) {
errorsTerminal.add(deviceErrors);
deviceErrors.setCurrentTerminalStatus(currentTerminalStatus);
this.currentTerminalErroRSService.saveCurrentTerminalErrors(deviceErrors);
}
for (final CurrentTerminalWarnings deviceWarnings : updateCurrentTerminalStatusRequest.getWarnings()) {
warningsTerminal.add(deviceWarnings);
deviceWarnings.setCurrentTerminalStatus(currentTerminalStatus);
this.currentTerminalWarningsService.saveCurrentTerminalWarnings(deviceWarnings);
}
currentTerminalStatus.setErrors(errorsTerminal);
currentTerminalStatus.setWarnings(warningsTerminal);
final User userUpdated = this.userService.saveUser(userFromAccesstoken);
//za da gi zemam aktivnite uredi za da gi imam pri prakanje na notifikacija
userUpdated.getTerminalActiveDevices().size();
//send notification on currentTerminalGlobalStatusOK = false
//if (sendNotification) {
//notificationService.sendEmailNotificationForCurrentTerminalStatus(userUpdated);
//}
return ResponseEntity.ok(userUpdated.getCurrentTerminalStatus());
}
响应日志:
28-01-2021 04:15:14.326 [http-nio-5000-exec-5] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_errors where id=?
28-01-2021 04:15:14.327 [http-nio-5000-exec-5] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_errors where id=?
28-01-2021 04:15:14.328 [http-nio-5000-exec-5] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_errors where id=?
28-01-2021 04:15:14.329 [http-nio-5000-exec-5] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_errors where id=?
28-01-2021 04:15:14.329 [http-nio-5000-exec-5] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_warnings where id=?
28-01-2021 04:15:14.329 [http-nio-5000-exec-5] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_warnings where id=?
28-01-2021 04:15:14.330 [http-nio-5000-exec-5] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_warnings where id=?
28-01-2021 04:15:14.330 [http-nio-5000-exec-5] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_warnings where id=?
这没问题,所有错误和警告都会被删除,但是当我在 compareTwoSets 中使用错误(从终端状态获取错误)时,我只会得到删除警告
响应日志:
28-01-2021 04:18:28.381 [http-nio-5000-exec-3] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_warnings where id=?
28-01-2021 04:18:28.385 [http-nio-5000-exec-3] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_warnings where id=?
28-01-2021 04:18:28.386 [http-nio-5000-exec-3] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_warnings where id=?
28-01-2021 04:18:28.387 [http-nio-5000-exec-3] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_warnings where id=?
如果我旋转这条线
this.currentTerminalErroRSService.deleteCurrentTerminalErrorsByCurrentTerminalStatusId(currentTerminalStatus.getId()); //delete all terminal errors
this.currentTerminalWarningsService.deleteCurrentTerminalWarningsByCurrentTerminalStatusId(currentTerminalStatus.getId()); //delete all terminal warnings
喜欢
this.currentTerminalWarningsService.deleteCurrentTerminalWarningsByCurrentTerminalStatusId(currentTerminalStatus.getId()); //delete all terminal warnings
this.currentTerminalErroRSService.deleteCurrentTerminalErrorsByCurrentTerminalStatusId(currentTerminalStatus.getId()); //delete all terminal errors
响应日志:
28-01-2021 04:20:29.190 [http-nio-5000-exec-1] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_errors where id=?
28-01-2021 04:20:29.191 [http-nio-5000-exec-1] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_errors where id=?
28-01-2021 04:20:29.191 [http-nio-5000-exec-1] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_errors where id=?
28-01-2021 04:20:29.192 [http-nio-5000-exec-1] DEBUG org.hibernate.sql.logStatement - delete from current_terminal_errors where id=?
当我从 terminalStatus 收到错误时会发生这种情况。
为什么会这样?
当我不使用错误时,一切正常。
请帮帮我。
感谢您的帮助
解决方案:
这2行
this.currentTerminalErroRSService.deleteCurrentTerminalErrorsByCurrentTerminalStatusId(currentTerminalStatus.getId());
this.currentTerminalWarningsService.deleteCurrentTerminalWarningsByCurrentTerminalStatusId(currentTerminalStatus.getId());
在 getErrors 和 getWarnings 之前,一切正常。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。