【问题标题】:Dozer nulls fields existing only in the dest instead of ignoringDozer 清空仅存在于 dest 中的字段,而不是忽略
【发布时间】:2018-05-13 16:59:40
【问题描述】:

我有一个 Pojo:

class Pojo{
   String name;
   String surname;
}

还有一个 jpa/hibernate 实体:

class Entity{
     Long id;
     String code;
     List<EntityPojo> ep;
}

最后是实体pojo

class EntityPojo{
    Long id;
    String name;
    String surname;
}

在我的代码中:

//I receive a pojo from the rest call and I want to update
Entity entity = repo.findByCode("aCode");

//at this point my entity pojo IDs are correctly filled!

dozerMapper.map(pojo, entity);

//after the map the IDs are null!!
repo.save(entity); //BAM!

如您所见,映射只是删除了 ID,这让我想到了休眠中的约束异常……这是为什么呢?

【问题讨论】:

    标签: java spring-data-jpa dozer


    【解决方案1】:

    您没有提供隔离和重现问题的代码。 根据您提供的内容,至少要检查以下内容:

    1) pojo 实例中的 id 是什么?如果它为空 - 这是你的结果。可能有用:Exclude Mapping Null Values

    2) 检查访问级别。如果字段不是公共的并且没有公共访问方法,我不确定 Dozer 目前是如何工作的。

    3) 您是否确认实体中的字段为空,而不是在数据库中?如果你这样做了 - 为什么这个问题有 spring-data-jpa 标记和与持久性级别相关的代码?如果你不这样做 - 为什么你确定 Dozer 有问题,而不是你的数据库映射?

    一般来说 - 您似乎根本不会自己解决这个问题,因为如果您这样做了 - 在检查它不是丢失您的地方之后,您至少会做一个清理数据库持久层的示例字段。很难回答一个包含多种技术的问题,而且问题可能无处不在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      相关资源
      最近更新 更多