개발/JPA

람다식 (stream,map,collect) 이유: 호환되지 않는 타입: ParticipantResponseDto는 Participant를 반환할 수 없습니다.

멋진놈 2022. 8. 23. 13:09
728x90

Service

List<ParticipantResponseDto> selectParticipant(final Long eventId) throws Exception;

 

ServiceImpl

public List<ParticipantResponseDto> selectParticipant(final Long eventId) throws Exception {

    List<ParticipantResponseDto> list = participantRepository.selectParticipant(eventId);

    return list.stream().map(ParticipantResponseDto::new).collect(Collectors.toList());
}

Repository

@Query(value = "select * from Participant where event_id = :id", nativeQuery = true)
List<ParticipantResponseDto> selectParticipant(@Param("id") Long id);

위와 같이 하면

이유: 호환되지 않는 타입: ParticipantResponseDto는 Participant를 반환할 수 없습니다.

 

해결 : 

 

Service

List<ParticipantResponseDto> selectParticipant(final Long eventId) throws Exception;

ServiceImpl

public List<ParticipantResponseDto> selectParticipant(final Long eventId) throws Exception {

    List<Participant> list = participantRepository.selectParticipant(eventId);

    return list.stream().map(ParticipantResponseDto::new).collect(Collectors.toList());
}

 

Repository

@Query(value = "select * from Participant where event_id = :id", nativeQuery = true)
List<Participant> selectParticipant(@Param("id") Long id);

 

public List<ParticipantResponseDto> selectParticipant(final Long eventId) throws Exception {

    List<Participant> list = participantRepository.selectParticipant(eventId);

    return list.stream().map(ParticipantResponseDto::new).collect(Collectors.toList());
}

해서 생긴 문제라고 봄.

람다식 (stream,map,collect)

ParticipantResponseDto에서는 파라미터로  participant.Repository에서 에 넘어온 결과 값을 Entity 를 participant로 받고 있으므로 => 결과적으로 .map(participant -> new ParticipantResponseDto(participant))가 되고 이것을

//collect를 사용해서 List로 변환한다. .collect(Collectors.toList());