Skip to content

Commit

Permalink
Feat: json 관련 어노테이션 - @JsonManagedReference & @JsonBackReference
Browse files Browse the repository at this point in the history
  • Loading branch information
bohub12 committed Sep 27, 2023
1 parent b00f21a commit 14ffa1d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 15 deletions.
46 changes: 31 additions & 15 deletions json-util/src/main/java/com/example/demo/JsonUtilApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.example.demo;

import com.example.demo.domain.Board;
import com.example.demo.domain.Comment;
import com.example.demo.domain.Member;
import com.example.demo.domain.Team;
import com.example.demo.infrastructure.BoardRepository;
import com.example.demo.infrastructure.CommentRepository;
import com.example.demo.infrastructure.MemberRepository;
import com.example.demo.infrastructure.TeamRepository;
import org.springframework.boot.CommandLineRunner;
Expand All @@ -12,20 +16,32 @@
@SpringBootApplication
public class JsonUtilApplication {

public static void main(String[] args) {
SpringApplication.run(JsonUtilApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner(MemberRepository memberRepository, TeamRepository teamRepository) {
return args -> {
Team team1 = teamRepository.save(new Team("teamA"));
Team team2 = teamRepository.save(new Team("teamB"));

memberRepository.save(new Member(team1, "member1", 10));
memberRepository.save(new Member(team2, "member2", 20));
memberRepository.save(new Member(team2, "member3", 30));
};
}
public static void main(String[] args) {
SpringApplication.run(JsonUtilApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner(MemberRepository memberRepository,
TeamRepository teamRepository,
BoardRepository boardRepository,
CommentRepository commentRepository) {
return args -> {
Team team1 = teamRepository.save(new Team("teamA"));
Team team2 = teamRepository.save(new Team("teamB"));

memberRepository.save(new Member(team1, "member1", 10));
memberRepository.save(new Member(team2, "member2", 20));
memberRepository.save(new Member(team2, "member3", 30));

Board board1 = boardRepository.save(new Board("title1", "content1"));
Board board2 = boardRepository.save(new Board("title2", "content2"));

commentRepository.save(new Comment(board1, "board1-comment1"));
commentRepository.save(new Comment(board1, "board1-comment2"));
commentRepository.save(new Comment(board1, "board1-comment3"));
commentRepository.save(new Comment(board2, "board2-comment1"));
commentRepository.save(new Comment(board2, "board2-comment2"));
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import com.example.demo.application.dto.jsonview.JsonViewResponse;
import com.example.demo.application.dto.jsonview.View.DetailResponse;
import com.example.demo.application.dto.jsonview.View.SummaryResponse;
import com.example.demo.domain.Board;
import com.example.demo.domain.Comment;
import com.example.demo.infrastructure.BoardRepository;
import com.example.demo.infrastructure.CommentRepository;
import com.example.demo.infrastructure.MemberRepository;
import com.example.demo.infrastructure.TeamRepository;
import com.fasterxml.jackson.annotation.JsonView;
Expand All @@ -35,6 +39,8 @@ public class CustomController {

private final MemberRepository memberRepository;
private final TeamRepository teamRepository;
private final BoardRepository boardRepository;
private final CommentRepository commentRepository;

@GetMapping("/property")
public JsonPropertyResponseData getDataUsingJsonProperty() {
Expand Down Expand Up @@ -116,5 +122,14 @@ public JsonViewResponse getDataUsingJsonViewByDetail() {
}


@GetMapping("/managed-reference")
public List<Board> getDataUsingJsonManagedReference() {
return boardRepository.findAll();
}

@GetMapping("/back-reference")
public List<Comment> getDataUsingJsonBackReference() {
return commentRepository.findAll();
}

}
35 changes: 35 additions & 0 deletions json-util/src/main/java/com/example/demo/domain/Board.java
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 json-util/src/main/java/com/example/demo/domain/Comment.java
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;
}
}
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> {

}
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> {

}

0 comments on commit 14ffa1d

Please sign in to comment.