Skip to content

[현서호] 1주차 과제 #9

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 1 commit into
base: main
Choose a base branch
from

Conversation

HYUN-SEO-HO
Copy link

image 표는 다음과 같이 작성했습니다. 운동에 관한 몇가지 정보와, 그와 관련된 메달을 표로 적고, 여러 테스트를 진행했습니다. 그런데 두 번째에서 findBy 부분이 잘 안됐습니다..ㅠ

Comment on lines 17 to +20
@Id // PK임을 나타낸다.
@GeneratedValue(strategy = GenerationType.AUTO) // 자동 생성되는 값임을 나타낸다.
@Column(name = "category_id") // 컬럼명을 지정한다.
private Long id;
//@GeneratedValue(strategy = GenerationType.AUTO) // 자동 생성되는 값임을 나타낸다.
@Column(name = "exercise") // 컬럼명을 지정한다.
private String exercise;
Copy link
Member

Choose a reason for hiding this comment

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

우선 수고하셨습니다! 코드 보니 어떠한 어려움이 있으셨을지 예상이 가서 몇가지 정보를 공유해드리겠습니다.

  1. Entity에서 @id는 해당 필드가 PK값임을 의미합니다. 따라서 유일하게 작성될 수 있도록 수동으로 작성하지 않는 편이 좋습니다.
  2. 숫자형 타입의 필드에 @id와 @GeneratedValue(strategy=GenerationType.AUTO)를 함께 사용하게 되면, 데이터베이스 단에서 자동으로 유일한 PK 값을 할당합니다. ( MySQL의 경우 이전의 PK 값 + 1)
  3. String 타입의 @id 필드에는 @GeneratedValue(strategy=GenerationType.AUTO)를 사용할 수 없습니다. 데이터베이스 단에서 이 값을 자동으로 생성할 수 없기 때문입니다.

따라서 아래처럼 작성하는 편이 조금 더 보편적입니다.

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


// @Column(name = "description")이 생략된 경우 필드명이 컬럼명이 된다.
private String description;
private String cORw;
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.

유산소 혹은 근력으로 표현하기 위해서 해당 필드를 사용하신 것 같습니다.
디테일하게 필드 작성하신 점 좋습니다!
추가로 다른 사람들도 보자마자 바로 알 수 있도록 이를 조금 더 이해하기 쉬운 이름으로 아래처럼 작성해주는 것이 어떨까 생각이 듭니다.

Suggested change
private String cORw;
private String exerciseType;

Comment on lines 10 to 17
// find + [ ] + By + (조건)

// select * from Category
Category findByDescription(String description);
// Category findByDescription(String description);

// select * from Category where type = ? and description = ?
Category findByTypeAndDescription(String type, String description);
// Category findByTypeAndDescription(String type, String description);
}
Copy link
Member

Choose a reason for hiding this comment

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

우선 클래스까지는 잘 생성해주셨습니다.
다만 이 쪽에서 JpaRepository를 사용하기 위해서는, JpaRepository<Entity Class 이름, 그 클래스의 PK 타입>으로 작성해야합니다. 현재까지 작성해주신 Entity는 String 타입이어서 아래와 같이 작성할 수 있습니다.

Suggested change
// find + [ ] + By + (조건)
// select * from Category
Category findByDescription(String description);
// Category findByDescription(String description);
// select * from Category where type = ? and description = ?
Category findByTypeAndDescription(String type, String description);
// Category findByTypeAndDescription(String type, String description);
}
public interface CategoryRepository extends JpaRepository<Category, String> {

Comment on lines 107 to +110
// 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(result1.getPlace()).isEqualTo("수영장");
Copy link
Member

Choose a reason for hiding this comment

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

검증하는 파트는 꼼꼼하게 잘 작성해주신 것 같습니다.
앞서 코멘트 달아두었던 내용들 반영해서 수정하신 뒤에, 다시 테스트 실행해보시면 좋을 것 같습니다!!

고생하셨습니다~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants