Spring Data Jpa rollback of Primary Key is not working

1k Views Asked by At

I am working in Spring Boot (2.1.7.RELEASE) + Spring Data JPA + postgres example and looking to rollback Primary Key Id in case of any exception occurs. I went through https://www.logicbig.com/tutorials/spring-framework/spring-data-access-with-jdbc/transactional-roll-back.html and How to rollback transaction in JPA? and many other useful links, but things did not worked for me.

In my project, I am always looking to store Unique Combination of firstName and LastName.

Student.java

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="STUDENT", uniqueConstraints = {
        @UniqueConstraint(name="STU_KEY",columnNames = {"FIRSTNAME", "LASTNAME"})
})
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="STUDENT_ID")
    private Long studentId;

    @Column(name="FIRSTNAME")
    private String firstName;

    @Column(name="LASTNAME")
    private String lastName;

    @Column(name="EMAIL")
    private String email;
}

I have developed REST endpoint

StudentController.java

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "Save Student", nickname = "Save Student")
    @ApiResponses(value = { @ApiResponse(code = 201, message = "Save Student Successful"),
            @ApiResponse(code = 500, message = "Internal Server Error"),
            @ApiResponse(code = 400, response = ErrorResource.class, message = "Program Type details are required ") })
    @PostMapping("/student")
    public ResponseEntity<HttpStatus> saveStudent(@ApiParam(value="Accepts a Student") @Valid @RequestBody StudentDto dto){
        studentService.saveStudent(dto);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
}

StudentServiceImpl.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import com.example.demo.dto.StudentDto;
import com.example.demo.entity.Student;
import com.example.demo.exceptions.InternalServerException;
import com.example.demo.repository.StudentRepository;

import lombok.extern.slf4j.Slf4j;


@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private Environment e;

    @org.springframework.transaction.annotation.Transactional(rollbackFor= {DataIntegrityViolationException.class, Exception.class})
    @Override
    public void saveStudent(StudentDto dto) {
        Student studentEntity = convertToEntity(dto);
        try {
            studentRepository.save(studentEntity);
        } catch (DataIntegrityViolationException e) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "ConstraintViolationException", e.getCause());
        } catch (Exception ex) {
            log.error(e.getProperty("error.errors"), e.getProperty("DB Exception"));
            throw new InternalServerException(HttpStatus.INTERNAL_SERVER_ERROR, e.getProperty("DB Exception"), ex);
        }
    }

    private Student convertToEntity(StudentDto dto) {
        return Student.builder().firstName(dto.getFirstName()).lastName(dto.getLastName()).email(dto.getEmail())
                .build();
    }
}

Below is the request Payload: First Time, it will save successfully into DB. Next time, I hit the request with same payload. UniqueConstraints failed, again I hit the same.

{
  "email": "[email protected]",
  "firstName": "John",
  "lastName": "Doe"
}

Now, this time I change payload to below and it saved into DB

{
  "email": "[email protected]",
  "firstName": "John1",
  "lastName": "Doe1"
}

But I see Primary Key Sequence No: 2 and 3 has been consumed. Is there any way to reset Primary Key 2 and 3 and when request is successful, then I wanted to save record at Primary Key 2.

student_id |email                |firstname |lastname |
-----------|---------------------|----------|---------|
1          |[email protected]   |John      |Doe      |
4          |[email protected] |John1     |Doe1     |

Let me know if any other info is needed, even I can share my sample code too.

0

There are 0 best solutions below