diff --git a/src/main/java/com/_1/spring_rest_api/entity/Keyword.java b/src/main/java/com/_1/spring_rest_api/entity/Keyword.java index 0f802f9..fbc6f74 100644 --- a/src/main/java/com/_1/spring_rest_api/entity/Keyword.java +++ b/src/main/java/com/_1/spring_rest_api/entity/Keyword.java @@ -24,4 +24,11 @@ public class Keyword { @ManyToOne @JoinColumn(name = "TEXT_id") private Text text; + + public void changeText(Text text) { + this.text = text; + if (text != null && !text.getKeywords().contains(this)) { + text.getKeywords().add(this); + } + } } diff --git a/src/main/java/com/_1/spring_rest_api/entity/Text.java b/src/main/java/com/_1/spring_rest_api/entity/Text.java index 9acb83c..5c36be3 100644 --- a/src/main/java/com/_1/spring_rest_api/entity/Text.java +++ b/src/main/java/com/_1/spring_rest_api/entity/Text.java @@ -2,12 +2,12 @@ import com._1.spring_rest_api.entity.base.BaseTimeEntity; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; import lombok.experimental.SuperBuilder; +import java.util.ArrayList; +import java.util.List; + @Entity @Table(name = "TEXT") @Getter @@ -21,10 +21,6 @@ public class Text extends BaseTimeEntity { @Column(name = "id") private Long id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "week_id") - private Week week; - @Column(name = "content", columnDefinition = "TEXT") private String content; @@ -35,6 +31,14 @@ public class Text extends BaseTimeEntity { @Column(name = "type") private String type; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "week_id") + private Week week; + + @OneToMany(mappedBy = "text", cascade = CascadeType.ALL, orphanRemoval = true) + @Builder.Default + private List keywords = new ArrayList<>(); + // Week와 Text 간의 양방향 연관관계 메서드 public void changeWeek(Week week) { this.week = week; @@ -43,6 +47,16 @@ public void changeWeek(Week week) { } } + public void addKeyword(Keyword keyword) { + this.keywords.add(keyword); + keyword.changeText(this); + } + + public void removeKeyword(Keyword keyword) { + this.keywords.remove(keyword); + keyword.changeText(null); + } + // 컨텐츠 업데이트 메서드 public void updateContent(String content) { this.content = content; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c23707d..bdc552c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,7 @@ spring.application.name=spring-rest-api spring.profiles.include=secret # \u00EC\u0084\u009C\u00EB\u00B2\u0084 \u00EC\u0084\u00A4\u00EC\u00A0\u0095 -server.port=8080 +server.port=8081 # JPA \u00EC\u0084\u00A4\u00EC\u00A0\u0095 spring.jpa.hibernate.ddl-auto=update