Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.

Scylla Java Driver Manual Mapper DAOs Null saving strategy

Null saving strategy¶

The null saving strategy controls how null entity properties are handled when writing to the database. It can be configured either for each method, or globally at the DAO level.

Two strategies are available:

  • DO_NOT_SET: the mapper won’t call the corresponding setter on the BoundStatement. The generated code looks approximately like this:

    if (entity.getDescription() != null) {
      boundStatement = boundStatement.setString("description", entity.getDescription());
    }
    

    This avoids inserting tombstones for null properties. On the other hand, if the query is an update and the column previously had another value, it won’t be overwritten.

    Note that unset values (CASSANDRA-7304) are only supported with native protocol v4 (Cassandra 2.2) or above . If you try to use this strategy with a lower Cassandra version, the mapper will throw an MapperException when you try to access the corresponding DAO.

  • SET_TO_NULL: the mapper will always call the setter, even with a null value. The generated code looks approximately like this:

    // Called even if entity.getDescription() == null
    boundStatement = boundStatement.setString("description", entity.getDescription());
    

Method level¶

Specify nullSavingStrategy on the method annotation:

import static com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy.SET_TO_NULL;

@Update(nullSavingStrategy = SET_TO_NULL)
void update(Product product);

This applies to @Insert, @Query, @SetEntity and @Update (other method types don’t need it since they don’t write data).

DAO level¶

Annotate your DAO interface with @DefaultNullSavingStrategy. Any method that does not explicitly define its strategy inherits the DAO-level one:

@Dao
@DefaultNullSavingStrategy(SET_TO_NULL)
public interface ProductDao {

  @Insert
  void insert(Product product); // inherits SET_TO_NULL

  @Update(nullSavingStrategy = DO_NOT_SET)
  void update(Product product); // uses DO_NOT_SET
}

If you don’t define a DAO-level default, any method that does not declare its own value defaults to DO_NOT_SET:

@Dao
public interface ProductDao {

  @Insert
  void insert(Product product); // defaults to DO_NOT_SET
}

Note that you can use inheritance to set a common default for all your DAOs:

@DefaultNullSavingStrategy(SET_TO_NULL)
public interface InventoryDao {}

@Dao
public interface ProductDao extends InventoryDao {
  @Insert
  void insert(Product product); // inherits SET_TO_NULL
}

@Dao
public interface UserDao extends InventoryDao {
  @Insert
  void insert(User user); // inherits SET_TO_NULL
}
PREVIOUS
Insert methods
NEXT
Query methods
  • 4.10.0.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • API conventions
    • Case sensitivity
    • Core driver
      • Address resolution
      • Asynchronous programming
      • Authentication
      • Bill of Materials (BOM)
      • Compression
      • Configuration
        • Reference configuration
      • Control connection
      • Custom codecs
      • Detachable types
      • Query idempotence
      • Integration
      • Load balancing
      • Logging
      • Metadata
        • Node metadata
        • Schema metadata
        • Token metadata
      • Metrics
      • Native protocol
      • Non-blocking programming
      • Paging
      • Performance
      • Connection pooling
      • Query timestamps
      • Reactive Style Programming
      • Reconnection
      • Request tracker
      • Retries
      • Using the shaded JAR
      • Speculative query execution
      • SSL
      • Statements
        • Batch statements
        • Per-query keyspace
        • Prepared statements
        • Simple statements
      • Temporal types
      • Request throttling
      • Query tracing
      • Tuples
      • User-defined types
    • Developer docs
      • Administrative tasks
      • Common infrastructure
        • Concurrency
        • Driver context
        • Event bus
      • Native protocol layer
      • Netty pipeline
      • Request execution
    • Mapper
      • Integration
        • Kotlin
        • Lombok
        • Java 14 Records
        • Scala
      • DAOs
        • Custom result types
        • Delete methods
        • GetEntity methods
        • Increment methods
        • Insert methods
        • Null saving strategy
        • Query methods
        • Query provider methods
        • Select methods
        • SetEntity methods
        • Statement attributes
        • Update methods
      • Entities
      • Mapper interface
    • OSGi
    • Query builder
      • Conditions
      • DELETE
      • Idempotence in the query builder
      • INSERT
      • Relations
      • Schema builder
        • Aggregate
        • Function
        • Index
        • Keyspace
        • Materialized View
        • Table
        • Type
      • SELECT
      • Terms
      • TRUNCATE
      • UPDATE
  • Upgrade guide
  • Frequently asked questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • Null saving strategy
    • Method level
    • DAO level
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2