14

I have used a delete method of Spring Data JPA in the service layer, but I wonder why neither the deleteById method nor delete method has any return values.

If we inspect the implementation of the delete method carefully, there is an if statement that when the entity to be deleted doesn't exist returns nothing.

public void delete(T entity) {

    Assert.notNull(entity, "Entity must not be null!");

    if (entityInformation.isNew(entity)) {
        return;
    }

    Class<?> type = ProxyUtils.getUserClass(entity);

    T existing = (T) em.find(type, entityInformation.getId(entity));

    // if the entity to be deleted doesn't exist, delete is a NOOP
    if (existing == null) {
        return;
    }

    em.remove(em.contains(entity) ? entity : em.merge(entity));
}

Personally, I think returning a Boolean value could be an adequate approach in this case because the controller layer will know about the deletion status, and the view layer can be provided with the far more reliable alert message.

4
  • If no exception is thrown during delete then everything is OK. Remember that you are operating JPQL not SQL. What reliable information do you expect? Apr 6, 2020 at 13:00
  • Because jpa neither returns no boolean ...
    – xerx593
    Apr 6, 2020 at 13:05
  • ..for this information (in jpa), you'd have to go for Query.executeUpdate():int/+ catch exceptions:) ..this returns (reliably) the count of modified/updated/deleted rows.
    – xerx593
    Apr 6, 2020 at 13:19
  • ..and spring-data can. docs.spring.io/spring-data/jpa/docs/current/reference/html/… (Abinash's answer)
    – xerx593
    Apr 6, 2020 at 13:29

3 Answers 3

17

Spring Data JPA design some build-in methods that way they think and give us the option to use the other way also. You can easily get deleted records and their count using derived delete query supported By Spring Data JPA (Reference)

@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
    Fruit deleteById(Long id); // To get deleted record
}
@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
    Long deleteById(Long id);  // To get deleted record count
}
5
  • Do you mean I can simply put Fruit deleteById(Integer id); in the repository and the rest will be done by Spring Data JPA and it will return the deleted record? I used the above-mentioned code and the requested entry of entity just deleted through the repository, and then return null (not the deleted record!), although it throws no exception. Apr 8, 2020 at 8:33
  • @ElyasHadizadeh Yes. Official document : Core concepts ->Example 6 docs.spring.io/spring-data/jpa/docs/current/reference/html/…
    – Eklavya
    Apr 8, 2020 at 9:29
  • 6
    The return type is incompatible with CrudRepository<TestoEntity,Long>.deleteById(Long) : does not work
    – Phate
    Nov 13, 2020 at 10:52
  • Hi can't see the update, is there a way to accomplish this?
    – Phate
    Nov 13, 2020 at 11:16
  • @dpelisek My intention is to show two ways, I am not saying you can use both in same repository. And I updated my answer to clear this.
    – Eklavya
    Jun 30, 2022 at 5:43
5

use @Modifying and @Query ant it will return number of deleted rows.

@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
    @Modifying
    @Query(value = "DELETE FROM Fruit f where f.id = ?1")
    int costumDeleteById(Long id);
}
0

Another option would be to follow the suggestion from this answer and check if the number of affected entities is 1 (in case of a deleteById method).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.