Skip to content

Commit

Permalink
#9 fix: Board, Category 엔티티 연관관계 OneToOne->ManyToOne 으로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaktmchl committed Oct 7, 2022
1 parent d3293a6 commit 5560d1c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class Board {
@OneToMany(mappedBy = "board")
private List<BoardImage> boardImages;

@OneToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;

Expand Down Expand Up @@ -118,4 +118,8 @@ public Board(CreateBoardReq dto, User host, City city, Category category){
public void changeBoardCurrentMember(int currentMember){
this.currentMember = currentMember;
}

public void addBoardUser(BoardUser boardUser){
this.boardUsers.add(boardUser);
}
}
10 changes: 10 additions & 0 deletions server/src/main/java/com/yogit/server/board/entity/BoardUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ public class BoardUser {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "board_id")
private Board board;

/*
연관관계 편의 메서드
*/

public BoardUser(User user, Board board) {
this.user = user;
this.board = board;
board.addBoardUser(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.List;

@Entity
@Getter
Expand All @@ -14,8 +15,8 @@ public class Category {
@Column(name = "category_id")
private Long id;

@OneToOne(mappedBy = "category")
private Board board;
@OneToMany(mappedBy = "category")
private List<Board> boards;

private String name;
private String example;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.yogit.server.board.dto.request.CreateBoardReq;
import com.yogit.server.board.dto.response.BoardRes;
import com.yogit.server.board.entity.Board;
import com.yogit.server.board.entity.BoardUser;
import com.yogit.server.board.entity.Category;
import com.yogit.server.board.exception.boardCategory.NotFoundCategoryException;
import com.yogit.server.board.repository.CategoryRepository;
Expand Down Expand Up @@ -38,16 +39,18 @@ public ApplicationResponse<BoardRes> createBoard(CreateBoardReq dto){
// city조회
City city = cityRepository.findById(dto.getCityId())
.orElseThrow(() -> new NotFoundCityException());
// boardUsers 조회
// boardImages 조회
// boardCategory 조회
// category 조회
Category category = categoryRepository.findById(dto.getCategoryId())
.orElseThrow(() -> new NotFoundCategoryException());

// board 생성
// board 객체 생성
Board board = new Board(dto, host, city, category);
board.changeBoardCurrentMember(0);// currentMember 디폴트=0
// board 생성 요청

// 호스트 boardUser 생성 및 board에 추가
// board.addBoardUser(new BoardUser(host, board));

// board 저장
Board savedBoard = boardRepository.save(board);
// resDto 벼환
BoardRes boardRes = BoardRes.toDto(savedBoard);
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ spring:
jpa:
database: mysql
hibernate:
ddl-auto: update
ddl-auto: create

0 comments on commit 5560d1c

Please sign in to comment.