Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/com/_1/spring_rest_api/entity/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
30 changes: 22 additions & 8 deletions src/main/java/com/_1/spring_rest_api/entity/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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<Keyword> keywords = new ArrayList<>();

// Week와 Text 간의 양방향 연관관계 메서드
public void changeWeek(Week week) {
this.week = week;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down