Was this page helpful?
ScyllaDB Java Driver is available under the Apache v2 License. ScyllaDB Java Driver is a fork of DataStax Java Driver. See Copyright here.
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());
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).
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
}
Was this page helpful?
ScyllaDB Java Driver is available under the Apache v2 License. ScyllaDB Java Driver is a fork of DataStax Java Driver. See Copyright here.