2

I am trying to add a list of rented books to my database.

I am using ModelMaper with SpringBoot.

Here is code:

RentDto.java

@Getter
@Setter
@EqualsAndHashCode
@ToString
@NoArgsConstructor
public class RentDto {

    private Long rentId;

    private Long userId;

    private Long bookId;

    private String userFirstName;

    private String userLastName;

    private String bookTitle;

    private LocalDateTime rentStart;

    private LocalDateTime RentEnd;

}

RentController.java

@ApiOperation(value = "Add Multiple Rents")
@PostMapping("/addMultipleRent")
@ResponseStatus(HttpStatus.CREATED)
public void addMultipleRent(@RequestBody List<RentDto> rentDto){
    rentService.addMultipleRents(rentDto);
}

RentService.java

 public void addMultipleRents(List<RentDto> rentDto){
            rentRepository.saveAll(mapRentListDtoToRentList(rentDto));
    }

    //Convert List of books to List of bookDto
    private List<Rent> mapRentListDtoToRentList(List<RentDto> rents) {
        return rents.stream()
                .map(rent -> modelMapper.map(rent, Rent.class))
                .collect(Collectors.toList());
    }

I am getting the following error:

1) The destination property com.example.library.entity.Rent.setId() matches multiple source property hierarchies:

    com.example.library.dto.RentDto.getRentId()
    com.example.library.dto.RentDto.getUserId()
    com.example.library.dto.RentDto.getBookId()

Any help appreciated.

user9347049
  • 1,927
  • 3
  • 27
  • 66

2 Answers2

1

This is the solution I found.

Added this line to my addMultipleRents() method.

List<Rent> rent = Arrays.asList(modelMapper.map(rentDto, Rent.class));

After I added it RentService.java looked like this:

public void addMultipleRents(List<RentDto> rentDto){
        List<Rent> rent = Arrays.asList(modelMapper.map(rentDto, Rent.class));
            rentRepository.saveAll(rent);
    }

I completely deleted method mapRentListDtoToRentList.

Dharman
  • 30,962
  • 25
  • 85
  • 135
user9347049
  • 1,927
  • 3
  • 27
  • 66
0

add an @Id annotation to your rentId field, sp spring knows which is THE id, that can also used for searching etc:

public class RentDto {

    @Id
    private Long rentId;

    //...
}
Jens Baitinger
  • 2,230
  • 14
  • 34