org.springframework.data.mapping.model.MappingInstantiationException:无法使用新的布尔字段实例化对象

问题描述 投票:0回答:3

我的

Spring-Boot
Couchbase
应用程序在数据库中有一个
Cat
对象。

在新的应用程序版本中,我们向

Cat
文档对象添加了新的布尔字段:

@RequiredArgsConstructor
@AllArgsConstructor(onConstructor = @__(@PersistenceConstructor))
@Document
@Data
@Builder
@EqualsAndHashCode
public class Cat {
....
@Field
final boolean isHungry

但是现在,我们已经在数据库中拥有了 Cat 对象,并且没有这个字段。

当应用程序尝试读取这些内容时

Cats
我们收到此错误:

org.springframework.data.mapping.model.MappingInstantiationException: Failed 
  to instantiate com.example.Cat using constructor public 
  com.example.Cat(...) with arguments ... 
...
Caused by: java.lang.IllegalArgumentException: Parameter isHungry must not be null!

有没有办法告诉

Spring
,如果数据库中缺少该字段,则应该使用默认值(在本例中为
false

java spring-boot couchbase spring-data-couchbase
3个回答
1
投票

尝试使用除

isHungry
之外的所有参数创建构造函数,并将
isHungry
设置为 false


1
投票

如何将

isHungry
更改为
Boolean
并添加一个 getter,将
null
映射到
false


0
投票

抱歉来晚了。就我而言,我应该排除注释生成器,之后我的新参数将被初始化为 false。

© www.soinside.com 2019 - 2024. All rights reserved.