package com.sparta.bloglogin.dto;

import com.sparta.bloglogin.entity.User;
import lombok.Getter;

import java.util.Comparator;
import java.util.List;

@Getter
public class ProfileResponseDto {
    private String username;
    // private String password;
    private String realname;
    private String introduction;
    private List<ProfilePostListResponseDto> posts;

    public ProfileResponseDto(User user) {
        this.username = user.getUsername();
        this.realname = user.getRealname();
        this.introduction = user.getIntroduction();
        this.posts = user.getPostList().stream().map(ProfilePostListResponseDto::new).sorted(Comparator.comparing(ProfilePostListResponseDto::getCreatedAt).reversed()).toList();
    }
}

유저 프로필에서 작성한 글 목록을 볼 수 있게 posts를 추가했더니 다음과 같은 오류가 발생했다.

 

방법 1. getMyPage에 @Transactional 걸어주기  ➡️  실패

 

방법 2. postList의 FetchType을 EAGER로 설정하기  ➡️  해결

    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
    private List<Post> postList;

 

프로필에서 작성글 목록 불러오기 성공!

+ Recent posts