Home > Software design >  How to make soft delete and set column deleted=true instead of real user deleting from database?
How to make soft delete and set column deleted=true instead of real user deleting from database?

Time:11-11

I want to make soft deleting by adding the additional column delete=true instead of real user deleting from database. I have the following method in UserServiceImpl:

@Override
public void deleteUser(String id) {
    UserEntity userEntity = userRepository.findById(Integer.valueOf(id))
            .orElseThrow(() -> new UserNotFoundException("Id not found"));
    if (userEntity.getLastAccessDate() == null) {
        throw new ProhibitedScimTypeException("Broken policy");
    }
    userRepository.delete(userEntity);
}

In UserController I have:

@ApiResponses(value = {
        @ApiResponse(responseCode = "204", content = {@Content(schema = @Schema(implementation = UserResource.class))}, description = "Successful")
})
@DeleteMapping("/Users/{id}")
public ResponseEntity<String> deleteUser(@PathVariable("id") String id) {
    userService.deleteUser(id);
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} 

And in UserRepository I've added the following method:

@Query("update UserEntity u set deleted = true where u = :u")
void delete(UserEntity userEntity);

Without this method in repository it didn't set true in deleted but using void delete I catch the mistake Using named parameters for method not found in annotated query 'update user set deleted = true where user = :user'. Could you give me the piece of advice - what is the correct way to make this soft deleted?

CodePudding user response:

Your reference to the parameter userEntity in your @Query is incorrect. It should be:

@Query("update UserEntity u set deleted = true where u = :userEntity")
  • Related