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

woobm2wo  于 5个月前  发布在  Spring
关注(0)|答案(3)|浏览(79)

我的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

kuhbmx9i

kuhbmx9i1#

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

xfyts7mz

xfyts7mz2#

如何将isHungry更改为Boolean并添加一个getter,它将Mapnullfalse

2w2cym1i

2w2cym1i3#

在我的例子中,我应该排除注解生成器,在此之后,我的新参数将被初始化为false。

相关问题