728x90
에러로그
hibernateLazyInitializer
문제 상황

보다시피 매우 엿같은 상황이 발생했다.
엔티티 매핑을 한 후 발생하는 에러다.
현재 상황은 Payment 엔티티에 @ManyToOne + Lazy적용, Battery 엔티티에 @OneToMany가 적용된 상황
좌측은 단방향만 개통된 상황 (Battery의 @OneToMany가 없을 때) : Lazy에 따른 오류
우측은 양방향이 개통된 상황 : 순환참조 문제
해결 방법
: Dto에서 필드를 선언해서 내보낼 때, 엔티티를 바로 내보내지 말고, Dto를 내보내라한다.
: 이게 옳은 방법인건지는 모르겠으나 필자는 이렇게 해결했다.
단일 객체 DTO 변환 방법
1. BatteryPayment클래스를 따로 만든다
2. 여기에 내가 필요한 값만 필드로 선언한다.
3. Response를 내보낼 Dto에 List<BatteryPayment>를 선언한다. (현재 Battery:Payment는 1:N 관계)
4. List에 BatteryPayment를 모두 때려박는 로직을 작성한다. (필자는 그냥 for문으로 때려밖았다)
Page 객체 DTO 변환 방법
1. 마찬가지로 원하는 정보만 다을 ResDto 클래스를 생성한다
2. 해당 ResDto안에 ResDto 생성자를 만들어두고, 컨트롤러 내 gets핸들러 메서드에서 map을 이용해 page를 통으로 변환시킨다.
@NoArgsConstructor
@Getter @Setter
public class StationBattery extends BaseTime {
private Long batteryId;
private String capacity;
private boolean status;
private int price;
private String photoURL;
}
@GetMapping
public ResponseEntity<PageInfoDto> getStations (Pageable pageable) {
Page<Station> page = stationService.getStations(pageable);
Page<StationResDto> dtoPage = page.map(StationResDto::new);
return new ResponseEntity<>(new PageInfoDto(dtoPage), HttpStatus.OK);
}
728x90
'Java & Spring > Error' 카테고리의 다른 글
| [Ngrok] Cors 이슈 해결하기 (0) | 2022.12.03 |
|---|---|
| [SpringBoot] SpringSecurity와 React Axios 간의 JWT 인식문제 (0) | 2022.11.29 |
| [SpringBoot] Caused by: java.sql.SQLSyntaxErrorException: Can't DROP 'FKpvill444mvu6ace1wvwpc9iob'; check that column/key exists 에러 (0) | 2022.11.18 |
| [SpringBoot] httpmessagenotwritableexception 에러 (0) | 2022.11.17 |
| [SpringBoot] DataIntegrityViolationException (0) | 2022.11.14 |