Jin's Dev Story

[스프링 부트 쇼핑몰 프로젝트 with JPA] 17-1. 게시판 - Entity 본문

Web & Android/스프링 부트 쇼핑몰 프로젝트 with JPA

[스프링 부트 쇼핑몰 프로젝트 with JPA] 17-1. 게시판 - Entity

woojin._. 2023. 10. 16. 20:12
이 내용은 스프링 부트 쇼핑몰 프로젝트 with JPA 책을 학습한 내용입니다.

Entity

  • Board

      @Entity
      @Getter
      @Setter
      @ToString
      @NoArgsConstructor
      @AllArgsConstructor
      @Table(name = "board")
      public class Board extends BaseTimeEntity {
    
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Column
          private Long id; // 게시판 번호 (자동 증가)
    
          @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "userId") // 외래키 설정
          private UserEntity user;  // 사용자 id
    
          // 시간 빼둠
    
          @Column(length = 50)
          private String title;  // 제목
    
          @Column(length = 1000)
          private String content;  // 내용
    
          @Column(columnDefinition = "int default 0")
          private int recommend; // 좋아요
    
          @JsonIgnore
          @BatchSize(size = 500)
          @OneToMany(mappedBy = "board", cascade = CascadeType.ALL, orphanRemoval = true)
          private List<Recommend> recommends;  // 좋아요 리스트
    
          @JsonIgnore
          @BatchSize(size = 500)
          @OneToMany(mappedBy = "board", cascade = CascadeType.ALL, orphanRemoval = true)
          private List<BoardComment> comments;  // 댓글 리스트
    
          public Board(UserEntity user, String title, String content) {
              this.user=user;
              this.title=title;
              this.content=content;
          }
    
          // 게시글 수정
          public Board update(BoardUpdateDTO boardUpdateDTO) {
              this.title=boardUpdateDTO.getTitle();
              this.content=boardUpdateDTO.getContent();
              return this;
          }
      }
  • BoardComment

      @Entity
      @Getter
      @Setter
      @ToString
      @NoArgsConstructor
      @AllArgsConstructor
      public class BoardComment extends BaseTimeEntity {
    
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Column
          private Long id; // 댓글 번호 (자동 증가)
    
          @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "boardId") // 외래키 설정
          private Board board;  // 게시판 번호
    
          @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "parentId")
          private BoardComment parent;
    
          // 시간 빼둠
    
          @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "userId") // 외래키 설정
          private UserEntity user;  // 사용자 id
    
          @Column(length = 500)
          private String content;  // 내용
    
          @Column(columnDefinition = "int default 0")
          private int recommend; // 댓글 좋아요
    
          @JsonIgnore
          @BatchSize(size = 500)
          @OneToMany(mappedBy = "boardComment", cascade = CascadeType.ALL, orphanRemoval = true)
          private List<RecommendComment> recommendComments;  // 좋아요 리스트
    
          @OneToMany(mappedBy = "parent", orphanRemoval = true)
          private List<BoardComment> children = new ArrayList<>();
    
          public BoardComment(UserEntity user, Board board, String content) {
              this.user=user;
              this.board=board;
              this.content=content;
          }
    
          public void updateParent(BoardComment parentComment) {
              this.parent=parentComment;
          }
    
          public BoardComment update(BoardCommentUpdateDTO boardCommentUpdateDTO) {
              this.content=boardCommentUpdateDTO.getContent();
              return this;
          }
      }
  • Recomend

      @Entity
      @Getter
      @Setter
      @ToString
      @IdClass(RecommendId.class)
      @NoArgsConstructor
      @AllArgsConstructor
      @Table(name = "recommend")
      public class Recommend {
    
          @Id
          @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "userId")
          private UserEntity user;
    
          @Id
          @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "boardId")
          private Board board;
      }
  • RecommendComment

      import com.example.walkingmate_back.user.entity.UserEntity;
      import jakarta.persistence.*;
      import lombok.*;
    
      @Entity
      @Getter
      @Setter
      @ToString
      @IdClass(RecommendCommentId.class)
      @NoArgsConstructor
      @AllArgsConstructor
      @Table(name = "recommendComment")
      public class RecommendComment {
    
          @Id
          @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "userId")
          private UserEntity user;
    
          @Id
          @ManyToOne(fetch = FetchType.LAZY)
          @JoinColumn(name = "boardCommentId")
          private BoardComment boardComment;
    
      }
  • RecommendId

      import com.example.walkingmate_back.user.entity.UserEntity;
      import lombok.AllArgsConstructor;
      import lombok.Data;
      import lombok.NoArgsConstructor;
      import java.io.Serializable;
    
      @Data
      @NoArgsConstructor
      @AllArgsConstructor
      public class RecommendId implements Serializable {
          private UserEntity user;
  • RecommendCommentId

    import com.example.walkingmate_back.user.entity.UserEntity;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import java.io.Serializable;
    

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RecommendCommentId implements Serializable {
private UserEntity user;
private BoardComment boardComment;
}
```