我的
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
)
尝试使用除
isHungry
之外的所有参数创建构造函数,并将 isHungry
设置为 false
如何将
isHungry
更改为 Boolean
并添加一个 getter,将 null
映射到 false
?
抱歉来晚了。就我而言,我应该排除注释生成器,之后我的新参数将被初始化为 false。