Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

java - Spring boot call delete method not work in some conditions

I have CurrentTerminalStatus entity like this:

@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,
fetch = FetchType.LAZY,
mappedBy = "currentTerminalStatus", orphanRemoval = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<CurrentTerminalWarnings> warnings;

........

getter/setter

}

CurrentTerminalErrors is:

@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

}

CurrentTerminalWarnings:

@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, optional = false)
@JoinColumn(name = "current_terminal_status_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private CurrentTerminalStatus currentTerminalStatus;

......

getter/setter

}

My controller:

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());
}

Response log:

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=?

This is ok and all errors and warnings is delete but when I use errors in the compareTwoSets (get errors from the terminal status) I get only delete warnings

Response log:

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=?

If I rotate this line

this.currentTerminalErrorsService.deleteCurrentTerminalErrorsByCurrentTerminalStatusId(currentTerminalStatus.getId());  //delete all terminal errors
        this.currentTerminalWarningsService.deleteCurrentTerminalWarningsByCurrentTerminalStatusId(currentTerminalStatus.getId()); //delete all terminal warnings

like

this.currentTerminalWarningsService.deleteCurrentTerminalWarningsByCurrentTerminalStatusId(currentTerminalStatus.getId()); //delete all terminal warnings

this.currentTerminalErrorsService.deleteCurrentTerminalErrorsByCurrentTerminalStatusId(currentTerminalStatus.getId());  //delete all terminal errors

only errors are deleted

Response log:

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=?

Insert is working but delete method is call but not execute.(Only first delete)

This happen when I get errors from terminalStatus.

Why is this happen?

When I not use errors all is working fine.

Please help me.

Thanks for all your help

Solution:

This 2 line

this.currentTerminalErrorsService.deleteCurrentTerminalErrorsByCurrentTerminalStatusId(currentTerminalStatus.getId());
        this.currentTerminalWarningsService.deleteCurrentTerminalWarningsByCurrentTerminalStatusId(currentTerminalStatus.getId());

is before getErrors and getWarnings and all is working fine.

question from:https://stackoverflow.com/questions/65930605/spring-boot-call-delete-method-not-work-in-some-conditions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...