我正在尝试将modelmapper与双向实体一起使用,这会给我一个StackOverFlow。
我的实体:
public class EntityA {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy="entityA", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private Set<EntityB> bSet = new HashSet<EntityB>(0);
public void setBSet(Set<EntityC> bSet ) {
bSet.forEach(f -> c.setEntityA(this));
this.bSet = bSet;
}
//Other setters and getters
}
public class EntityB {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "entityA_id"))
private EntityA entityA;
@OneToMany(mappedBy="entityB", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private Set<EntityC> cSet = new HashSet<EntityC>(0);
public void setCSet(Set<EntityC> cSet ) {
cSet .forEach(f -> c.setEntityB(this));
this.cSet = cSet ;
}
//Other setters and getters
}
public class EntityC {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "entityB_id"))
private EntityB entityB;
//setters and getters
}
我的DTO
public class DtoA {
private Long id;
private Set<DtoB> bSet = new HashSet<DtoB>(0);
//setters and getters
}
public class DtoB{
private Long id;
private Set<DtoC> cSet = new HashSet<DtoC>(0);
//setters and getters
}
public class DtoC {
private Long id;
//setters and getters
}
因此,当我执行以下代码时,实体的递归性就会产生stackoverflow。
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
modelMapper.getConfiguration().map(dto, EntityA.class);
我尝试添加此配置,但问题仍然存在:
modelMapper.getConfiguration().setFieldAccessLevel(AccessLevel.PRIVATE);
modelMapper.getConfiguration().setFieldMatchingEnabled(true);
我该如何解决?