ModelMapper 映射错误的 id
ModelMapper mapping the wrong id
我在将 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
您可以在 link 的 'Skipping Properties' 部分找到如何使用 skip() 。
http://modelmapper.org/javadoc/org/modelmapper/PropertyMap.html
我在将 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
您可以在 link 的 'Skipping Properties' 部分找到如何使用 skip() 。 http://modelmapper.org/javadoc/org/modelmapper/PropertyMap.html