Skip to content
This repository was archived by the owner on Aug 13, 2022. It is now read-only.

Category 영역에 대한 JPA Migration #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
developmentOnly 'org.springframework.boot:spring-boot-devtools'

// validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import kr.fiveminutesmarket.category.service.MainCategoryService;
import kr.fiveminutesmarket.common.dto.ResponseDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

@RestController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/subCategory")
Expand All @@ -20,22 +19,6 @@ public SubCategoryController(SubCategoryService subCategoryService) {
this.subCategoryService = subCategoryService;
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public ResponseDto<List<SubCategoryResponse>> getAll() {
List<SubCategoryResponse> subCategoryList = subCategoryService.findAll();

return new ResponseDto<>(0,null, subCategoryList);
}

@GetMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public ResponseDto<SubCategoryResponse> findById(@PathVariable("id") Long id) {
SubCategoryResponse subCategory = subCategoryService.findById(id);

return new ResponseDto<>(0,null, subCategory);
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ResponseDto<SubCategoryResponse> add(@Valid @RequestBody SubCategoryRequest resource) {
Expand All @@ -47,10 +30,10 @@ public ResponseDto<SubCategoryResponse> add(@Valid @RequestBody SubCategoryReque
@PutMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public ResponseDto<?> update(@PathVariable("id") Long id,
@Valid @RequestBody SubCategoryRequest resource) {
@Valid @RequestBody SubCategoryRequest resource) {
subCategoryService.update(id, resource);

return new ResponseDto<>(0);
return new ResponseDto<>(0, "수정 완료하였습니다.");
}

@DeleteMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package kr.fiveminutesmarket.category.domain;

import kr.fiveminutesmarket.category.dto.request.MainCategoryRequest;
import kr.fiveminutesmarket.category.dto.response.MainCategoryResponse;

public class MainCategory {
import javax.persistence.*;
import java.util.List;

@Entity
public class MainCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long mainCategoryId;

private String mainCategoryName;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE ,mappedBy = "mainCategory")
private List<SubCategory> subCategoryList;
Comment on lines +16 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subCategoryList가 Lazy로 fetch하면 얻는 이점이 어떤게 있을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@f-lab-bright
lazy loading은 DB에서 조회해 연관된 엔티티를 한꺼번에 영속성 컨텍스트에 올려놓는 것이 아니라 실제로 subCatgory를 사용할 때 DB조회하기 때문에 불필요한 쿼리 낭비가 없다는 것이 장점 중에 하나입니다.

그런데 MainCategory 조회시 보통 SubCategory도 같이 사용된다는 점에서 EAGER, 즉시로딩으로 하는 것도 좋다고 생각합니다!
(보통 페이지에서 Category 조회시에도 SubCategory까지 같이 조회한다는 것을 고려)


public MainCategory() {
}

Expand All @@ -24,15 +31,12 @@ public String getMainCategoryName() {
return mainCategoryName;
}

public List<SubCategory> getSubCategoryList() {
return subCategoryList;
}

public void updateInfo(MainCategoryRequest resource) {
this.mainCategoryName = resource.getMainCategoryName();
}

public MainCategoryResponse toResponse() {
MainCategoryResponse response = new MainCategoryResponse();
response.setMainCategoryId(mainCategoryId);
response.setMainCategoryName(mainCategoryName);

return response;
}
}
37 changes: 17 additions & 20 deletions src/main/java/kr/fiveminutesmarket/category/domain/SubCategory.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package kr.fiveminutesmarket.category.domain;

import kr.fiveminutesmarket.category.dto.request.SubCategoryRequest;
import kr.fiveminutesmarket.category.dto.response.SubCategoryResponse;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class SubCategory {
import javax.persistence.*;

@Entity
public class SubCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long subCategoryId;

private String subCategoryName;

private Long mainCategoryId;
@ManyToOne
@JoinColumn(name = "main_category_id")
@JsonIgnore
private MainCategory mainCategory;

public SubCategory() {
}

public SubCategory(String subCategoryName, Long mainCategoryId) {
public SubCategory(String subCategoryName, MainCategory mainCategory) {
this.subCategoryName = subCategoryName;
this.mainCategoryId = mainCategoryId;
this.mainCategory = mainCategory;
}

public Long getSubCategoryId() {
Expand All @@ -27,21 +33,12 @@ public String getSubCategoryName() {
return subCategoryName;
}

public Long getMainCategoryId() {
return mainCategoryId;
}

public SubCategoryResponse toResponse() {
SubCategoryResponse response = new SubCategoryResponse();
response.setSubCategoryId(subCategoryId);
response.setSubCategoryName(subCategoryName);
response.setMainCategoryId(mainCategoryId);

return response;
public MainCategory getMainCategory() {
return mainCategory;
}

public void updateInfo(SubCategoryRequest resource) {
this.subCategoryName = resource.getSubCategoryName();
this.mainCategoryId = resource.getMainCategoryId();
public void updateInfo(String subCategoryName, MainCategory mainCategory) {
this.subCategoryName = subCategoryName;
this.mainCategory = mainCategory;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kr.fiveminutesmarket.category.dto.request;

import kr.fiveminutesmarket.category.domain.MainCategory;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
Expand All @@ -24,8 +23,4 @@ public String getMainCategoryName() {
return mainCategoryName;
}

public MainCategory toEntity() {
return new MainCategory(mainCategoryName);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kr.fiveminutesmarket.category.dto.request;

import kr.fiveminutesmarket.category.domain.SubCategory;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
Expand Down Expand Up @@ -30,8 +29,4 @@ public String getSubCategoryName() {
public Long getMainCategoryId() {
return mainCategoryId;
}

public SubCategory toEntity() {
return new SubCategory(subCategoryName, mainCategoryId);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package kr.fiveminutesmarket.category.dto.response;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.List;

public class MainCategoryResponse {

private Long mainCategoryId;

private String mainCategoryName;

public Long getMainCategoryId() {
return mainCategoryId;
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<SubCategoryResponse> subCategoryResponses;

public MainCategoryResponse() {
}

public void setMainCategoryId(Long mainCategoryId) {
public MainCategoryResponse(Long mainCategoryId, String mainCategoryName, List<SubCategoryResponse> subCategoryResponses) {
this.mainCategoryId = mainCategoryId;
this.mainCategoryName = mainCategoryName;
this.subCategoryResponses = subCategoryResponses;
}

public Long getMainCategoryId() {
return mainCategoryId;
}

public String getMainCategoryName() {
return mainCategoryName;
}

public void setMainCategoryName(String mainCategoryName) {
this.mainCategoryName = mainCategoryName;
public List<SubCategoryResponse> getSubCategoryResponses() {
return subCategoryResponses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,20 @@ public class SubCategoryResponse {

private String subCategoryName;

private Long mainCategoryId;

public Long getSubCategoryId() {
return subCategoryId;
public SubCategoryResponse() {
}

public void setSubCategoryId(Long subCategoryId) {
public SubCategoryResponse(Long subCategoryId, String subCategoryName) {
this.subCategoryId = subCategoryId;
}

public String getSubCategoryName() {
return subCategoryName;
}

public void setSubCategoryName(String subCategoryName) {
this.subCategoryName = subCategoryName;
}

public Long getMainCategoryId() {
return mainCategoryId;
public Long getSubCategoryId() {
return subCategoryId;
}

public void setMainCategoryId(Long mainCategoryId) {
this.mainCategoryId = mainCategoryId;
public String getSubCategoryName() {
return subCategoryName;
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
package kr.fiveminutesmarket.category.repository;

import kr.fiveminutesmarket.category.domain.MainCategory;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
public interface MainCategoryRepository extends JpaRepository<MainCategory, Long> {

@Mapper
public interface MainCategoryRepository {

int insert(@Param("mainCategory") MainCategory mainCategory);

List<MainCategory> findAll();

MainCategory findById(@Param("mainCategoryId") Long mainCategoryId);

int countByName(@Param("mainCategoryName") String mainCategoryName);

int updateMainCategory(@Param("mainCategoryId") Long mainCategoryId,
@Param("mainCategory") MainCategory mainCategory);

void deleteById(@Param("mainCategoryId") Long mainCategoryId);
long countByMainCategoryName(String mainCategoryName);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
package kr.fiveminutesmarket.category.repository;

import kr.fiveminutesmarket.category.domain.SubCategory;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
public interface SubCategoryRepository extends JpaRepository<SubCategory, Long> {

@Mapper
public interface SubCategoryRepository {

int insert(@Param("subCategory") SubCategory subCategory);

List<SubCategory> findAll();

SubCategory findById(@Param("subCategoryId") Long subCategoryId);

int countByName(@Param("subCategoryName") String subCategoryName);

int updateSubCategory(@Param("subCategoryId") Long subCategoryId,
@Param("subCategory") SubCategory subCategory);

void deleteById(@Param("subCategoryId") Long subCategoryId);

void deleteByMainCategoryId(@Param("mainCategoryId") Long mainCategoryId);
long countBySubCategoryName(String subCategoryName);
}
Loading