4

I understand that for the current version(2.x.x) of spring-data-commons it is guaranteed that the return value of CrudRepository.save() will never be null. However, what about the older versions, specifically 1.13.18.RELEASE?

If the older version does guarantee return of non-null values then it renders the following piece of code useless, right?

        Entity savedEntity = someRepository.save(entity);
        if (savedEntity == null) {
            // throw some checked exception stating that the save failed..
        }

And if the older version doesn't guarantee return of a non-null value, then what scenario would lead to the return of a null value?

Update: Since my question is pertaining to the implementation of CrudRepository, it is important to point out that I am using 1.11.18.RELEASE of spring-data-jpa. I want to know about the behaviour of save function for this version.

WillyWonka
  • 91
  • 1
  • 7
  • It doesn't look like either enforce non null returns. I can't see anything in the code to suggest it isn't entirely up to the implementation to decide this. – 123 Apr 04 '20 at 14:29
  • @123 can you elaborate? Are you trying to say that even the current/latest version doesn't enforce non-null returns? If that is the case then isn't the documentation misleading? – WillyWonka Apr 05 '20 at 06:16

2 Answers2

1

same issue here. Even though it should never return null, in my project I get a null value (silent failure) when I update an existing entity. If I create a new entity it works. I tried also implementing the Persistable interface, but still the same issue. I am using Spring Data + R2DBC + Java 15.

Davide Pugliese
  • 366
  • 7
  • 16
0

According to implementation, it will always return a value or throw an exception

Update the implementation is for 1.11.18.RELEASE. From GitHub repository

@Transactional
public <S extends T> S save(S entity) {
    if (this.entityInformation.isNew(entity)) {
        this.em.persist(entity);
        return entity;
    } else {
        return this.em.merge(entity);
    }
}
Ihar Sadounikau
  • 741
  • 6
  • 20
  • I have updated my answer to include details of the implementation I am using. The link you shared, I believe, is for the latest version `2.x.x`. – WillyWonka Apr 05 '20 at 06:34