Hej
Mam taką encję i takie DTO:
@Entity
@Table(name = "Tracks")
public class Track {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@NotBlank(message = "The track name cannot be blank")
private String name;
@NotNull
@Min(value = 1, message = "Total length of the track must be greater than 0")
private Integer lengthInSeconds;
@NotBlank(message = "The track genre cannot be blank")
private String genre;
@ManyToOne
@JoinColumn(name = "fk_id_album")
private Album album;
Track() {}
// getery i setery
}
public class TrackDto {
@NotBlank(message = "The track name cannot be blank")
private String name;
@NotNull
@Min(value = 1, message = "Total length of the track must be greater than 0")
private int lengthInSeconds;
@NotBlank(message = "The track genre cannot be blank")
private String genre;
@NotBlank(message = "The album title cannot be blank")
private String albumTitle;
public TrackDto() {
}
// getery i setery
}
Album ma wiele pól, chciałbym aby moje DTO miało tylko tytuł albumu
Jak to zrobić przy takim mapowaniu Entity -> DTO:
ModelMapper modelMapper = new ModelMapper();
Track track = repository.findById(12)
.orElseThrow(() -> new IllegalArgumentException("Track with given id not found")); //jakiś tam track
TrackDto trackDto = modelMapper.map(track, TrackDto.class);
Coś takiego nie zapisze mi w ogóle albumu, nie wiem jak skonfigurować ten modelMapper aby z pola Album wyciągał string 'title' i zapisywał w 'albumTitle' z TrackDto.