Post 애그리거트 수정 전

@Getter
@NoArgsConstructor
@Entity
public class Post extends BaseTimeEntity {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    @Column(length = 500, nullable = false)
    private String title;

    @Column(columnDefinition = "text", nullable = false)
    private String content;

    @Column(nullable = false)
    private String author;

    @Enumerated(EnumType.STRING)
    private TagType category;

    @Builder
    public Post(String title, String content, String author, TagType category) {
        this.title = title;
        this.content = content;
        this.author = author;
        this.category = category;
    }

    public void update(String title, String content){
        this.title = title;
        this.content = content;
    }
}

Post 애그리거트 수정 후

@Getter
@NoArgsConstructor
@Entity
public class Post extends BaseTimeEntity {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long postId;
    
    @Column(length = 500, nullable = false)
    private String title;

    @Column(columnDefinition = "text", nullable = false)
    private String content;

    @ManyToOne(fetch = FetchType.LAZY)  
    @JoinColumn(name= "user_id", nullable = false)
    private User user;

    @Enumerated(EnumType.STRING)
    private TagType category;

    @Builder
    public Post(String title, String content, User user, TagType category) {
        this.title = title;
        this.content = content;
        this.user= user;
        this.category = category;
    }

    public void update(String title, String content){
        this.title = title;
        this.content = content;
    }
}

Post 애그리거트에 User 애그리거트를 직접 참조시키는 이유

PostService save 메서드 수정 전

 @Transactional
    public Long save(PostCreateRequestDto requestDto, SessionUser sessionUser){
        requestDto.setUserName(sessionUser.getName());
        if(requestDto.getUserName()==null){
            throw new IllegalArgumentException("작성자가 누락되었습니다.");
        }
        return postsRepository.save(requestDto.toEntity()).getId();
    }

PostService의 save 메서드 수정 후