Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #119 from News-Sseuk/FIX#78
Browse files Browse the repository at this point in the history
[FIX] 양방향 참조로 인한 레디스 순환참조 문제 (stackoverflow)를 DTO로 해결
  • Loading branch information
gahyun02 authored Nov 16, 2024
2 parents 95db2ad + 69963c8 commit 028efeb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backend.newssseuk.springbootmongodb.dto;

import backend.newssseuk.domain.articleHashTag.ArticleHashTag;
import backend.newssseuk.springbootmongodb.redis.ArticleHashTagDTO;
import backend.newssseuk.springbootmongodb.redis.ArticleRedisEntity;
import com.mongodb.lang.Nullable;
import lombok.*;
Expand All @@ -25,7 +26,7 @@ public class ArticleResponseDto {

private String category;

private List<ArticleHashTag> hashTagList;
private List<ArticleHashTagDTO> hashTagList;

private Integer reliability;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package backend.newssseuk.springbootmongodb.redis;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ArticleHashTagDTO {
private Long id;
private Long articleId; // Article의 ID만 저장
private String hashTagName; // HashTag 이름만 저장
private LocalDateTime createdTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -36,7 +38,16 @@ public ArticleRedisEntity cashingArticles(String id) {
.content(article.get().getContent())
.category(article.get().getCategory().getKorean())
.publishedDate(article.get().getPublishedDate().format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm"))) // yyyy.MM.dd HH:mm 형식으로 변경하기
.hashTagList(jpaArticle.getArticleHashTagList())
.hashTagList(Optional.ofNullable(jpaArticle.getArticleHashTagList())
.orElse(Collections.emptyList())
.stream()
.map(articleHashTag -> ArticleHashTagDTO.builder()
.id(articleHashTag.getId())
.articleId(articleHashTag.getArticle().getId()) // Article ID만 사용
.hashTagName(articleHashTag.getHashTag().getName()) // HashTag 이름 사용
.createdTime(articleHashTag.getCreatedTime())
.build())
.collect(Collectors.toList()))
.reliability(jpaArticle.getReliability())
.summary(jpaArticle.getSummary())
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Builder
@Getter
@Setter
@ToString
@ToString(exclude = "hashTagList")
@AllArgsConstructor
@NoArgsConstructor
@RedisHash(value="article")
Expand All @@ -34,7 +34,7 @@ public class ArticleRedisEntity {
@Indexed
private String category;

private List<ArticleHashTag> hashTagList;
private List<ArticleHashTagDTO> hashTagList;

private Integer reliability;

Expand Down

0 comments on commit 028efeb

Please sign in to comment.