【发布时间】:2020-03-24 07:36:54
【问题描述】:
早安
我正在使用 ModelMapper 将 DTO 映射到实体,反之亦然,与此相关我有一个问题。
1) 当我从 SonController 获取 SonDTO 时,我需要将 long momId 映射到 Entity Son,但是我有 Entity Mother Mother 建立关系并且在内部具有 id。 那么如何将这个 SonDTO momId 映射到 Entity Mother Mother 中,反之亦然?
类下面:
class SonDTO {
long id;
String name;
int age;
long motherId;
}
class MotherDTO{
long id;
String name;
int age;
List<Long> sonsId;
List<String> sonsName;
}
@Entity
class Mother{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
@OneToMany(mappedBy = "mother", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
List<Son> sons;
}
@Entity
class Son{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mother_id")
private Mother mother;
}
【问题讨论】:
-
你在这里遇到什么问题??
-
我正在尝试使用 modelMapper 将 SonDTO 中的 motherId 映射到具有此属性但我做不到的母实体
标签: java spring spring-boot jpa spring-data-jpa