代码之家  ›  专栏  ›  技术社区  ›  riorio

org.springframework.data.mapping.model.mappingInstantiationException:未能用新的布尔字段实例化对象

  •  1
  • riorio  · 技术社区  · 6 年前

    我的 Spring-Boot 和; Couchbase 应用程序有一个 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 在这种情况下)

    1 回复  |  直到 6 年前
        1
  •  1
  •   aBnormaLz    6 年前

    尝试使用除 isHungry 和集合 伊桑格雷