[英]Spring Boot Modelmapper - StackOverflowError: null
Im getting 1) Error mapping entity.Team to TeamDTO followed by java.lang.StackOverflowError: null without any really cause or line in the stacktrace.我得到 1) 错误将 entity.Team 映射到 TeamDTO,后跟 java.lang.StackOverflowError: null 没有任何真正的原因或堆栈跟踪中的行。 Im trying to fetch an entity from database by Id.
我试图通过 Id 从数据库中获取一个实体。
Entity:实体:
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Team implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String teamName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_league", referencedColumnName = "id", nullable = false)
private League league;
@OneToMany(mappedBy="homeTeam")
List<Match> homeTeamMatches = new ArrayList<>();
@OneToMany(mappedBy="awayTeam")
List<Match> awayTeamMatches = new ArrayList<>();
@Column(name = "created_at", nullable = false, updatable = false)
private Date createdAt;
@Column(name = "updated_at", nullable = false)
private Date updatedAt;
public Team(String teamName, League league) {
this.teamName = teamName;
this.league = league;
}
@PrePersist
private void createdAt() {
this.createdAt = new Date();
this.updatedAt = new Date();
}
@PreUpdate
private void updatedAt() {
this.updatedAt = new Date();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Team team = (Team) o;
return id.equals(team.id) &&
teamName.equals(team.teamName);
}
@Override
public int hashCode() {
return Objects.hash(id, teamName);
}
}
DTO for the Entity:实体的 DTO:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({"homeTeamMatches", "awayTeamMatches", "league"})
public class TeamDTO {
private Long id;
private String teamName;
private LeagueDTO league;
List<MatchDTO> homeTeamMatches = new ArrayList<>();
List<MatchDTO> awayTeamMatches = new ArrayList<>();
private Date createdAt;
private Date updatedAt;
}
Here Im fetching the data and converting to DTO to return it back to the controller:我在这里获取数据并转换为 DTO 以将其返回给控制器:
private final TeamRepository teamRepository;
private final ModelMapper modelMapper = new ModelMapper();
public TeamService(@Autowired TeamRepository teamRepository) {
this.teamRepository = teamRepository;
}
public TeamDTO findById(Long id) {
return convertToCategoryDTO(teamRepository.findById(id).get());
}
private TeamDTO convertToCategoryDTO(Team team) {
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.LOOSE);
return modelMapper
.map(team, TeamDTO.class);
}
So can anyone help me??所以有人可以帮我吗??
EDIT: Found out that this two lines in my TeamDTO causes the stackoverflow:编辑:发现我 TeamDTO 中的这两行导致了计算器溢出:
List<MatchDTO> homeTeamMatches = new ArrayList<>();
List<MatchDTO> awayTeamMatches = new ArrayList<>();
How to map these properties two without causing an stackoverflow?如何在不导致计算器溢出的情况下映射这两个属性?
The private final ModelMapper modelMapper = new ModelMapper();
private final ModelMapper modelMapper = new ModelMapper();
is not a good practice in Spring context.在 Spring 上下文中不是一个好习惯。
you need to create a separate config class as below,您需要创建一个单独的配置类,如下所示,
@Configuration
public class AppConfiguration {
@Bean
public ModelMapper modelMapper(){
return new ModelMapper();
}
}
and use并使用
@Autowired
private final ModelMapper modelMapper;
or in setter injection.或在二传手注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.