我得到1)错误Mapentity.team到teamdto后跟java.lang.StackOverflower:null,stacktrace中没有任何真正的原因或行。我正试图从数据库中提取一个实体的id。
实体:
@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:
@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;
}
在这里,我提取数据并转换为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);
}
有人能帮我吗??
编辑:发现我的团队中的这两行导致堆栈溢出:
List<MatchDTO> homeTeamMatches = new ArrayList<>();
List<MatchDTO> awayTeamMatches = new ArrayList<>();
如何在不引起堆栈溢出的情况下Map这两个属性?
1条答案
按热度按时间tktrz96b1#
这个
private final ModelMapper modelMapper = new ModelMapper();
在spring环境中不是一个好的实践。您需要创建一个单独的配置类,如下所示,
使用
或在塞特注射。