
게시글과 게시글 첨부파일이 OneToMany 관계 속에서 articleAttachment에 Article 객체를 넣어주지 않을 경우, exception 발생
BigInteger partnerId = securityUser.getUserId();
Board board = boardRepository.findOneByName(body.getBoardName())
.orElseThrow(() -> new ApiException(ExceptionEnum.RESOURCE_NOT_FOUND));
Base user = userRepository.findOneById(partnerId)
.orElseThrow(() -> new ApiException(ExceptionEnum.RESOURCE_NOT_FOUND));
Article article = articleMapper.toEntity(body);
article.setOwner(user);
article.setBoard(board);
articleRepository.save(article);
위 코드에서 아래 코드로 변경해주었습니다.
attachment가 article을 알 수 있도록 하는 로직을 추가하니 db에 잘 추가됩니다!
BigInteger partnerId = securityUser.getUserId();
Board board = boardRepository.findOneByName(body.getBoardName())
.orElseThrow(() -> new ApiException(ExceptionEnum.RESOURCE_NOT_FOUND));
Base user = userRepository.findOneById(partnerId)
.orElseThrow(() -> new ApiException(ExceptionEnum.RESOURCE_NOT_FOUND));
Article article = articleMapper.toEntity(body);
article.setOwner(user);
article.setBoard(board);
if (body.getAttachments() != null) {
article.getAttachments().forEach(a -> a.setArticle(article));
}
if (body.getImages() != null) {
article.getImages().forEach(i -> i.setArticle(article));
}
articleRepository.save(article);

