-
Notifications
You must be signed in to change notification settings - Fork 14
[현서호] 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
base: main
Are you sure you want to change the base?
[현서호] 1주차 과제 #9
Conversation
HYUN-SEO-HO
commented
Oct 1, 2024

@Id // PK임을 나타낸다. | ||
@GeneratedValue(strategy = GenerationType.AUTO) // 자동 생성되는 값임을 나타낸다. | ||
@Column(name = "category_id") // 컬럼명을 지정한다. | ||
private Long id; | ||
//@GeneratedValue(strategy = GenerationType.AUTO) // 자동 생성되는 값임을 나타낸다. | ||
@Column(name = "exercise") // 컬럼명을 지정한다. | ||
private String exercise; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
우선 수고하셨습니다! 코드 보니 어떠한 어려움이 있으셨을지 예상이 가서 몇가지 정보를 공유해드리겠습니다.
- Entity에서 @id는 해당 필드가 PK값임을 의미합니다. 따라서 유일하게 작성될 수 있도록 수동으로 작성하지 않는 편이 좋습니다.
- 숫자형 타입의 필드에 @id와 @GeneratedValue(strategy=GenerationType.AUTO)를 함께 사용하게 되면, 데이터베이스 단에서 자동으로 유일한 PK 값을 할당합니다. ( MySQL의 경우 이전의 PK 값 + 1)
- String 타입의 @id 필드에는 @GeneratedValue(strategy=GenerationType.AUTO)를 사용할 수 없습니다. 데이터베이스 단에서 이 값을 자동으로 생성할 수 없기 때문입니다.
따라서 아래처럼 작성하는 편이 조금 더 보편적입니다.
@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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
유산소 혹은 근력으로 표현하기 위해서 해당 필드를 사용하신 것 같습니다.
디테일하게 필드 작성하신 점 좋습니다!
추가로 다른 사람들도 보자마자 바로 알 수 있도록 이를 조금 더 이해하기 쉬운 이름으로 아래처럼 작성해주는 것이 어떨까 생각이 듭니다.
private String cORw; | |
private String exerciseType; |
// 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); | ||
} |
There was a problem hiding this comment.
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 타입이어서 아래와 같이 작성할 수 있습니다.
// 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> { |
// 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("수영장"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
검증하는 파트는 꼼꼼하게 잘 작성해주신 것 같습니다.
앞서 코멘트 달아두었던 내용들 반영해서 수정하신 뒤에, 다시 테스트 실행해보시면 좋을 것 같습니다!!
고생하셨습니다~