WillyWonka
WillyWonka

Reputation: 133

Does CrudRepository.save return null for spring data 1.13.18.RELEASE

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.

Upvotes: 4

Views: 3453

Answers (2)

Davide Pugliese
Davide Pugliese

Reputation: 386

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.

Upvotes: 1

Ihar Sadounikau
Ihar Sadounikau

Reputation: 751

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);
    }
}

Upvotes: 0

Related Questions