-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: json 관련 어노테이션 - @JsonManagedReference & @JsonBackReference
- Loading branch information
Showing
6 changed files
with
129 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
json-util/src/main/java/com/example/demo/domain/Board.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.example.demo.domain; | ||
|
||
import com.fasterxml.jackson.annotation.JsonManagedReference; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Board { | ||
|
||
@Id @GeneratedValue | ||
private Long id; | ||
private String title; | ||
private String content; | ||
|
||
@OneToMany(mappedBy = "board") | ||
@JsonManagedReference | ||
private List<Comment> comments = new ArrayList<>(); | ||
|
||
public Board(String title, String content) { | ||
this.title = title; | ||
this.content = content; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
json-util/src/main/java/com/example/demo/domain/Comment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.demo.domain; | ||
|
||
import com.fasterxml.jackson.annotation.JsonBackReference; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Comment { | ||
|
||
@Id @GeneratedValue | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JsonBackReference | ||
private Board board; | ||
|
||
private String content; | ||
|
||
public Comment(Board board, String content) { | ||
this.board = board; | ||
this.content = content; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
json-util/src/main/java/com/example/demo/infrastructure/BoardRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.demo.infrastructure; | ||
|
||
import com.example.demo.domain.Board; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BoardRepository extends JpaRepository<Board, Long> { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
json-util/src/main/java/com/example/demo/infrastructure/CommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.demo.infrastructure; | ||
|
||
import com.example.demo.domain.Comment; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
||
} |