Home > Software design >  org.springframework.dao.EmptyResultDataAccessException -->Help me fix this
org.springframework.dao.EmptyResultDataAccessException -->Help me fix this

Time:07-31

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

CodePudding user response:

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 }

  • Related