简体   繁体   English

Datastax Java驱动程序nullable = false

[英]Datastax Java-Driver nullable=false

I am using datastax java-driver for cassandra. 我正在为Cassandra使用Datastax Java驱动程序。 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. 在hibernate / JPA中,我们具有带有@Column批注的nullable = false属性。 Such attribute doesn't exist with datastax driver 此类属性在datastax驱动程序中不存在

Can I achieve nullable = false behavior with datastax java driver for cassandra ? 我可以使用cassandra的datastax java驱动程序实现nullable = false行为吗?

Cassandra Version : 2.1.8 cassandra-driver-core : 3.1.0 Cassandra版本:2.1.8 cassandra-driver-core:3.1.0

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. 如果您希望驱动程序在尝试将null值插入nullable = false列时引发错误,则类似于JPA,则没有这种功能。

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: 但是,如果只需要防止将空值插入到数据库中,则可以选中以下选项: SaveNullFields ,其用法如下:

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. 这在upsert操作的情况下特别有用,在upsert操作中,您不希望空值替换现有数据。

Documentation: Datastax: Using the mapper 文档: Datastax:使用映射器

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. saveNullFields :如果设置为true,则在要保留的实例中值为null的字段将在查询中显式写入null。 If set to false, fields with null value won't be included in the write query (thus avoiding tombstones). 如果设置为false,则具有空值的字段将不会包含在写入查询中(因此避免了逻辑删除)。 If not specified, the default behavior is to persist null fields. 如果未指定,则默认行为是保留空字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM