Open
Conversation
HYUN-SEO-HO
commented
Oct 1, 2024
표는 다음과 같이 작성했습니다.
운동에 관한 몇가지 정보와, 그와 관련된 메달을 표로 적고, 여러 테스트를 진행했습니다.
그런데 두 번째에서 findBy 부분이 잘 안됐습니다..ㅠ
jjunhub
reviewed
Oct 2, 2024
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; |
Member
There was a problem hiding this comment.
우선 수고하셨습니다! 코드 보니 어떠한 어려움이 있으셨을지 예상이 가서 몇가지 정보를 공유해드리겠습니다.
- Entity에서 @id는 해당 필드가 PK값임을 의미합니다. 따라서 유일하게 작성될 수 있도록 수동으로 작성하지 않는 편이 좋습니다.
- 숫자형 타입의 필드에 @id와 @GeneratedValue(strategy=GenerationType.AUTO)를 함께 사용하게 되면, 데이터베이스 단에서 자동으로 유일한 PK 값을 할당합니다. ( MySQL의 경우 이전의 PK 값 + 1)
- 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; |
jjunhub
reviewed
Oct 2, 2024
|
|
||
| // @Column(name = "description")이 생략된 경우 필드명이 컬럼명이 된다. | ||
| private String description; | ||
| private String cORw; |
Member
There was a problem hiding this comment.
유산소 혹은 근력으로 표현하기 위해서 해당 필드를 사용하신 것 같습니다.
디테일하게 필드 작성하신 점 좋습니다!
추가로 다른 사람들도 보자마자 바로 알 수 있도록 이를 조금 더 이해하기 쉬운 이름으로 아래처럼 작성해주는 것이 어떨까 생각이 듭니다.
Suggested change
| private String cORw; | |
| private String exerciseType; |
jjunhub
reviewed
Oct 2, 2024
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); | ||
| } |
Member
There was a problem hiding this comment.
우선 클래스까지는 잘 생성해주셨습니다.
다만 이 쪽에서 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> { |
jjunhub
reviewed
Oct 2, 2024
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("수영장"); |
Member
There was a problem hiding this comment.
검증하는 파트는 꼼꼼하게 잘 작성해주신 것 같습니다.
앞서 코멘트 달아두었던 내용들 반영해서 수정하신 뒤에, 다시 테스트 실행해보시면 좋을 것 같습니다!!
고생하셨습니다~
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.