Skip to content

오단비 1주차 과제 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/main/java/doit/jpastudy2/Jpastudy2Application.java
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@
public class Jpastudy2Application {

public static void main(String[] args) {

System.out.println("hello world!");
SpringApplication.run(Jpastudy2Application.class, args);
}

Original file line number Diff line number Diff line change
@@ -12,22 +12,27 @@
@Entity
@NoArgsConstructor
@Getter
public class Category {
public class Army {

@Id // PK임을 나타낸다.
@GeneratedValue(strategy = GenerationType.AUTO) // 자동 생성되는 값임을 나타낸다.
@Column(name = "category_id") // 컬럼명을 지정한다.
private Long id;

// @Column(name = "type")이 생략된 경우 필드명이 컬럼명이 된다. snake_case로 변환된다.
private String type;
@Column(name = "군번") // 컬럼명을 지정한다.
private int id;

// @Column(name = "description")이 생략된 경우 필드명이 컬럼명이 된다.
@Column(name = "이름")
private String name;

@Column(name="부대")
private String militaryUnit;

@Column(name = "보직")
private String description;

Comment on lines +20 to 31
Copy link
Member

Choose a reason for hiding this comment

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

주제가 재밌네요ㅎㅎ..
Column명을 한국어로 작성하여도 동작은 정상적으로 하지만, 보통은 영어로 적는 편이 관례입니다.
참고하시면 될 것 같아요!

@Builder // 빌더 패턴을 사용할 수 있게 한다.
public Category(String description, String type) {
public Army(String name, String militaryUnit, String description) {
this.name = name;
this.militaryUnit = militaryUnit;
this.description = description;
this.type = type;
}
}
14 changes: 14 additions & 0 deletions src/main/java/doit/jpastudy2/repository/ArmyRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package doit.jpastudy2.repository;

import org.springframework.data.jpa.repository.JpaRepository;

Copy link
Member

@jjunhub jjunhub Oct 2, 2024

Choose a reason for hiding this comment

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

여기 부분 이렇게 작성하셔도 동작은 하는데, 보통은 <Entity Class 이름, 그 클래스의 PK 타입>으로 작성하는 편입니다.
jpa단에서 자동으로 int <-> Long 변환을 수행하지만, 처음부터 올바르게 적어주시는게 좋을 것 같습니다~

Suggested change
public interface ArmyRepository extends JpaRepository<Army, int> {

public interface ArmyRepository extends JpaRepository<Army, Long> {

// 쿼리 메소드 패턴은 다음과 같다.
// [ ] = Optional
// ( ) = 조건
// find + [ ] + By + (조건)

Army findByDescription(String description);
Army findByName(String name);
}
17 changes: 0 additions & 17 deletions src/main/java/doit/jpastudy2/repository/CategoryRepository.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/doit/jpastudy2/repository/Personal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package doit.jpastudy2.repository;

import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

public class Personal {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)

@Column(name="군번")
private int id;

@Column(name = "출생년도")
private int birthYear;

@Column(name = "생일")
private int birthDay;

@Column(name="전화번호")
private String phoneNumber;

}
83 changes: 36 additions & 47 deletions src/test/java/doit/jpastudy2/repository/CategoryRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package doit.jpastudy2.repository;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
@@ -12,97 +10,88 @@

@Transactional // 테스트 케이스에 이 어노테이션이 있으면, 테스트가 끝나면 롤백을 해준다. ( 데이터베이스 초기화 )
@SpringBootTest // 스프링 컨테이너를 이용한 테스트
class CategoryRepositoryTest {
class ArmyRepositoryTest {

@Autowired // 스프링이 관리하는 빈을 주입받는다.
private CategoryRepository categoryRepository;
private ArmyRepository categoryRepository;

@DisplayName("save 테스트")
@Test
void test() {
// Given
Category category1 = Category.builder()
.type("양식")
.description("데이트")
Army army1 = Army.builder()
.description("이발병")
.build();

Category category2 = Category.builder()
.type("한식")
.description("한국인의 정")
Army army2 = Army.builder()
.description("통신병")
.build();

// When
categoryRepository.save(category1);
categoryRepository.save(category2);
categoryRepository.save(army1);
categoryRepository.save(army2);

// Then
List<Category> categories = categoryRepository.findAll();
List<Army> categories = categoryRepository.findAll();
Assertions.assertThat(categories).hasSize(2);
Assertions.assertThat(categories.get(0).getType()).isEqualTo("양식");
Assertions.assertThat(categories.get(0).getDescription()).isEqualTo("데이트");
Assertions.assertThat(categories.get(1).getDescription()).isEqualTo("통신병");
}

@DisplayName("Description을 이용한 조회")
@Test
void findByDescription() {
// Given
Category category1 = Category.builder()
.type("양식")
.description("데이트")
Army army1 = Army.builder()
.description("행정병")
.build();

Category category2 = Category.builder()
.type("한식")
.description("한국인의 정")
Army army2 = Army.builder()
.description("운전병")
.build();

categoryRepository.save(category1);
categoryRepository.save(category2);
categoryRepository.save(army1);
categoryRepository.save(army2);

// When
Category result1 = categoryRepository.findByDescription("철가방");
Category result2 = categoryRepository.findByDescription("데이트");
Army result1 = categoryRepository.findByDescription("통신병");
Army result2 = categoryRepository.findByDescription("운전병");

// Then
Assertions.assertThat(result1).isNull();
Assertions.assertThat(result2).isNotNull();
Assertions.assertThat(result2.getType()).isEqualTo("양식");
}

@DisplayName("description과 type을 이용한 조회")
@DisplayName("name을 이용한 조회")
@Test
void findByTypeAndDescription() {
void findByName() {
// Given
Category category1 = Category.builder()
.type("양식")
.description("데이트")
Army army1 = Army.builder()
.name("박준혁")
.build();

Category category2 = Category.builder()
.type("한식")
.description("한국인의 정")
Army army2 = Army.builder()
.name("노승현")
.build();

Category category3 = Category.builder()
.type("중식")
.description("철가방")
Army army3 = Army.builder()
.name("설만수")
.build();

Category category4 = Category.builder()
.type("미식")
.description("축구ㅋㅋ")
Army army4 = Army.builder()
.name("현서호")
.build();

categoryRepository.saveAll(List.of(category1, category2, category3, category4));

categoryRepository.saveAll(List.of(army1, army2, army3, army4));

// When
Category result1 = categoryRepository.findByTypeAndDescription("양식", "데이트");
Category result2 = categoryRepository.findByTypeAndDescription("중식", "데이트"); // null
Category result3 = categoryRepository.findByTypeAndDescription("미식", "축구ㅋㅋ");
Army result1 = categoryRepository.findByName("오단비");
Army result2 = categoryRepository.findByName("박준혁");
Army result3 = categoryRepository.findByName("노승현");

// Then
Assertions.assertThat(result1.getType()).isEqualTo("양식");
Assertions.assertThat(result2).isNull();
Assertions.assertThat(result3.getDescription()).isEqualTo("축구ㅋㅋ");
Assertions.assertThat(result1).isNull();
Assertions.assertThat(result2).isNotNull();
Assertions.assertThat(result3.getName()).isEqualTo("노승현");
Comment on lines +93 to +95
Copy link
Member

Choose a reason for hiding this comment

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

굿굿굿입니다.
깔끔하게 잘 해내신 것 같습니다! 👍

}
}