gpt4 book ai didi

spring - 在 Spring 中使用 CrudRepositoty 保存具有 NULL 值的实体

转载 作者:行者123 更新时间:2023-12-01 12:26:35 25 4
gpt4 key购买 nike

使用 Spring Boot、Cassandra、org.springframework.data.repository.CrudRepository 来保存实体。

我有下一个实体:

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

存储库:

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

保存实体:

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

CrudRepository 不会将具有 NULL 值的属性写入数据库,因为

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

在MappingCassandraConverter.java

有没有办法使用 CrudRepository 保存 NULL 值?

最佳答案

已于 6 月 28 日修复。看看下面的网址 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.

当前版本是

<version>1.4.2.RELEASE</version>

但是我认为这种变化已经发生

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

您可以等待下一个正式版本,也可以访问此网址 http://projects.spring.io/spring-data-cassandra/#quick-start并在下载旁边的下拉菜单中选择 1.5.0 快照,而不是 1.4.2。如果系统正在生产中,我建议您等待下一个正式版本。

希望这有帮助

关于spring - 在 Spring 中使用 CrudRepositoty 保存具有 NULL 值的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39006275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com