Vitalii Sky
Vitalii Sky

Reputation: 133

Save entity with NULL value using CrudRepositoty in Spring

Using Spring Boot, Cassandra, org.springframework.data.repository.CrudRepository for save entity.

I have next entity:

@Table("user")
public class User implements Serializable {
    @PrimaryKey
    private UUID id;
    private String name;
...
}

Repository:

public interface UserRepository extends CrudRepository<User, UUID> {}

Saving entity:

user.setName(null);
userRepository.save(user);

CrudRepository doesn't write attribute with NULL value to database because

if (value != null) {
                    log.debug(String.format("Adding insert.value [%s] - [%s]", prop.getColumnName().toCql(), value));
                    insert.value(prop.getColumnName().toCql(), value);
                }

in MappingCassandraConverter.java

Are There some way to save NULL value using CrudRepository?

Upvotes: 2

Views: 5091

Answers (2)

Daniel
Daniel

Reputation: 1696

I'm using 1.5.3.RELEASE,but the problem still exists.

if (value == null) {
    return;
}

if (log.isDebugEnabled()) {
    log.debug("Adding insert.value [{}] - [{}]", property.getColumnName().toCql(), value);
}           

Upvotes: 1

Periklis Douvitsas
Periklis Douvitsas

Reputation: 2491

it is fixed on June 28th. Have a look on the following url https://github.com/spring-projects/spring-data-cassandra/pull/72

We now support the removal of columns for properties with null values by allowing null values in Insert and Update statements written by MappingCassandraConverter. Objects are stored with all persistent properties. Insert and update perform no longer conditional inserting of non-null values but take all values into account.

The current release is

<version>1.4.2.RELEASE</version>

however i think that this change is on

<version>1.5.0.BUILD-SNAPSHOT</version>

You can either wait for the next official release or you can go to this url http://projects.spring.io/spring-data-cassandra/#quick-start and in the drop down menu next to the download select instead of 1.4.2 the 1.5.0 snapshot. If the system is in production I will advise you to wait for the next official release.

Hope this helps

Upvotes: 4

Related Questions