Reputation: 3898
I am using datastax java-driver for cassandra. For one of the column in a table i wish to prevent null values during insert/update.
In hibernate/JPA we have nullable = false attribute with @Column annotation. Such attribute doesn't exist with datastax driver
Can I achieve nullable = false behavior with datastax java driver for cassandra ?
Cassandra Version : 2.1.8 cassandra-driver-core : 3.1.0
Upvotes: 0
Views: 1229
Reputation: 264
If you expect the driver to throw an error when you try to insert a null value to an nullable = false column, similarly to JPA, there is no such functionality.
However, if you just need to prevent null values to be inserted to your DB, you may check the option: SaveNullFields, which is used as follows:
yourEntityMapper.setDefaultSaveOptions(Option.saveNullFields(false));
This is particularly useful in case of upsert operations, where you do not want null values to replace existing data.
Documentation: Datastax: Using the mapper
saveNullFields: if set to true, fields with value null in an instance that is to be persisted will be explicitly written as null in the query. If set to false, fields with null value won’t be included in the write query (thus avoiding tombstones). If not specified, the default behavior is to persist null fields.
Upvotes: 3