본문 바로가기

개발 일지

[TIL]이노베이션 캠프 63일차

1. 개발 진행 상황

 

 // 코스 게시글 작성(카드 이미지 통합)
    @Transactional
    public PostResponseDto postCreate(PostPlaceDto postPlaceDto, List<MultipartFile> image, Member member)throws IOException {
        List<String> imgPaths  = s3Uploader.uploadList(image);
        System.out.println("IMG 경로들 : " + imgPaths);
        //uploadList에서 받은 이미지 경로 리스트를 하나씩 빼서 첫번째는 post에 나머지는 place에 하나씩 할당해줘야함
        String postImage = null;
        List<String> placeImage = new ArrayList<>(1);
            //만약 imgPaths의 길이가 0이면
            for (int i = 0; i < imgPaths.size(); i++) {
                if (i == 0) {
                    postImage = imgPaths.get(i);
                }else{
                    placeImage.add(i-1, imgPaths.get(i));

                }
            }

        Post post = new Post(postPlaceDto.getPostRequestDto(), postImage, member);
        Long courseId = postRepository.save(post).getId();
        for (int i =0; i <postPlaceDto.getPlaceRequestDtoList().size(); i++){
            placeService.placeCreate(courseId, postPlaceDto.getPlaceRequestDtoList().get(i), placeImage);
        }
        return new PostResponseDto(post);
    }

 

 

package com.example.week08.dto.response;

import com.example.week08.domain.Place;
import com.example.week08.domain.Post;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class PostResponseDto {
    private Long id;
    private String title;
    private String content;
    private String image;
    private String weather;
    private String region;
    private String season;
    private String who;
    private double avgScore;
    private int heart;
    private Long memberId;
    private String nickname;
    private String profileImage;
    private String location;
    private LocalDateTime createdAt;
    private LocalDateTime modifiedAt;
    private List<Place> place;

    public PostResponseDto(Post post) {
        this.id = post.getId();
        this.title = post.getTitle();
        this.content = post.getContent();
        this.image = post.getImage();
        this.weather = post.getWeather();
        this.region = post.getRegion();
        this.season = post.getSeason();
        this.who = post.getWho();
        this.avgScore = post.getAvgScore();
        this.heart = post.getHeart();
        this.place = post.getPlace();
        this.memberId = post.getMember().getId();
        this.nickname = post.getMember().getNickname();
        this.profileImage = post.getMember().getProfileImage();
        this.location = post.getMember().getLocation();
        this.createdAt = post.getCreatedAt();
        this.modifiedAt = post.getModifiedAt();
    }


}

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.example.week08.domain.Post.place, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.example.week08.domain.Post.place, could not initialize proxy - no Session (through reference chain: com.example.week08.dto.response.PostResponseDto["place"])

사용할 때 영속성 컨텍스트가 종료되어 지연 로딩을 할 수 없어서 발생하는 오류

 

2. 개발 중 발생한 이슈와 해결

3. 새로 배운 것

4. 참고 레퍼런스

5. 오늘 한 일 / 회고

6. TO-DO LIST

7. 특이사항