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());
'개발 > JPA' 카테고리의 다른 글
@Builder (0) | 2022.08.26 |
---|---|
JPA jpql을 사용하면서 배운것. 아주 중요. 그리고 나중에 생각 꼭 (0) | 2022.08.23 |
기존에 자동으로 받아오는 table 값을 값을 받아서 오게 끔 처리하는 법. (0) | 2022.08.19 |
JPA - java if 객체 long null 조건으로 사용법 (0) | 2022.08.12 |
JPA String 필요한 제공 타입 TIL 에러 좋은 정보 나중에 안될 때 확인 (0) | 2022.08.12 |