java - ModelMapper 映射错误的 id

标签 java spring dto modelmapper

我在将 dto 映射到具有嵌套对象的实体时遇到问题。 基本上一个“DayEntry”包含许多“SingleEntry”,每个“SingleEntry”都有一个“Category”。 这是我的实体:

@Entity
public @Data class SingleEntry {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id ;
    private float value ;
    private boolean isInEntry= true;
    @ManyToOne
    @JoinColumn(nullable=false)
    private Category category;
    @ManyToOne
    @JoinColumn(nullable=false)
    private DayEntry dayEntry;
}
@Entity
public @Data class Category {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;
    @Column(unique = true)
    private String name;
    private String color ="cc8282";
    @OneToMany(mappedBy = "category")
    //@JsonManagedReference
    private List<SingleEntry> singleEntryList;
}
@Entity
public @Data class DayEntry {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;
    @Column(nullable = false)
    private LocalDate date;
    @OneToMany(mappedBy = "dayEntry", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<SingleEntry> singleEntryList;

}

这是我正在使用的 Dto:

public @Data class SingleEntryForCreationDto {

    private float value ;
    private boolean isInEntry;
    private int categoryId;
    private int dayEntryId;
}

我正在尝试创建一个新的“SingleEntity”,这就是为什么我在 dto 中没有“id”,因为它将被生成,但是当我使用模型映射器时:

modelMapper.map(singleEntryForCreationDto, SingleEntry.class)

它将“categoryId”映射到实体“SingleEntry”的“id”字段,就像您在这个日志示例中看到的那样:

********* dto == SingleEntryForCreationDto(value=50.0, isInEntry=false, categoryId=2, dayEntryId=1)

********* mapped dto == SingleEntry(id=2, value=50.0, isInEntry=false, category=Category(id=2, name=null, color=cc8282, singleEntryList=null), dayEntry=DayEntry(id=1, date=null, singleEntryList=null))

我查看了 Modelmapper 的文档并尝试更改 NamingConventions 和 Transormation 配置,但没有帮助。我一定是遗漏了一些东西,而且我肯定不明白 Modelmapper 的真正工作原理,因为我无法自行修复它。

如果有任何帮助或建议,我将不胜感激。

******编辑: 我仍然不明白为什么它将categoryId(SingleEntryForCreationDto)映射到id(SingleEntry)。我暂时这样解决了

modelMapper.typeMap(SingleEntryForCreationDto.class,SingleEntry.class).addMappings(mapper -> mapper.skip(SingleEntry::setId));

最佳答案

我找不到为什么你的配置设置 SingleEntry 的 id 2 的值,但我认为你最好使用显式映射。

您可以使用 PropertyMap 配置跳过目标中的任何字段。

显式映射示例:没有跳过但 map() https://github.com/modelmapper/modelmapper/blob/master/examples/src/main/java/org/modelmapper/flattening/example2/FlatteningExample2.java

您可以在此链接的“跳过属性”部分找到如何使用 skip()。 http://modelmapper.org/javadoc/org/modelmapper/PropertyMap.html

关于java - ModelMapper 映射错误的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64078284/

相关文章:

java - 自定义 'String' 类

java - TestNG:在 Eclipse 中的 DataProvider 中返回 Iterator 时出现 ClassCastException

java - Spring Bean 类可以包含静态方法吗?

java - Spring Data JPA - javax.persistence.TransactionRequiredException : Executing an update/delete query

Spring Boot Java 映射实体到 DTO : array literal (strings) INSTEAD of array of objects

java - 如何将 WebSocket 与 Jetty 嵌入式自定义服务器一起使用

java - 在 Ubuntu 上从 CMD 安装 Groovy 后未链接的 java jar

spring - 在 Spring Boot 中从 @RestController 有条件地返回 JSON 和 (HTML) 模板

java - 获取 com.thoughtworks.xstream.converters.ConversionException 将 xml 列表转换为 DTO

java - Hibernate:返回不是实体的DTO?