Skip to content
Advertisement

org.springframework.dao.EmptyResultDataAccessException –>Help me fix this

public String deleteCounterParty(List<CounterParty>counterParties)
{
    String message = "";
    for(CounterParty counterParty:counterParties) {
        if (counterParty.getId() != null && counterPartyRepository.getById(counterParty.getId()) != null) {
            counterPartyRepository.deleteById(counterParty.getId());
            message="deleted successfully";
        }
        else {
            message="id not found";
        }
    }

  return message;

}

I’m using this method to delete rows by giving list of ids if the given id is found it is deleted,if not it throws this EmptyResultDataAccessException help me fix this

Advertisement

Answer

Data access exception thrown when a result was expected to have at least one row (or element) but zero rows (or elements) were actually returned.

Use a try-catch block to catch the exception if thrown and alter your logic accordingly so that when the code follows a happy path, at least one row is returned.

try { //code to delete rows } catch(final EmptyResultDataAccessException e) { //exception message }

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement