我的Spring-Boot
& Couchbase
应用程序在数据库中有一个Cat
对象。
在新的应用程序版本中,我们为out 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
,如果DB中缺少该字段,它应该使用默认值(在本例中为false
)
3条答案
按热度按时间kuhbmx9i1#
尝试使用除
isHungry
之外的所有参数创建构造函数,并将isHungry
设置为falsexfyts7mz2#
如何将
isHungry
更改为Boolean
并添加一个getter,它将Mapnull
到false
?2w2cym1i3#
在我的例子中,我应该排除注解生成器,在此之后,我的新参数将被初始化为false。