๋ฐ์ํ
1. Entity ์์ฑ
๋ฏธ๋ฆฌ ์์ฑ๋ ERD๋ฅผ ๋ฐํ์ผ๋ก ๋๋ฉ์ธ ์์ฑ
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Todo extends BaseEntity {
@Id @GeneratedValue
private Long seq;
@Builder
public Todo(String name, String content, TodoStatus status, Integer predictedPomo, Integer realPomo, WorkProficiency workProficiency, WorkType workType, WorkImportance workImportance) {
this.name = name;
this.content = content;
this.status = status;
this.predictedPomo = predictedPomo;
this.realPomo = realPomo;
this.workProficiency = workProficiency;
this.workType = workType;
this.workImportance = workImportance;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="group_seq")
private Group group;
private String name, content;
private TodoStatus status;
private Integer predictedPomo, realPomo;
private WorkProficiency workProficiency;
private WorkType workType;
private WorkImportance workImportance;
}
[ Annotation ]
- @Entity: ํ
์ด๋ธ์ ๋์ํ๋ ํ๋์ ํด๋์ค
- ๊ธฐ๋ณธ ์์ฑ์ ํ์! → JPA๊ฐ ์ํฐํฐ ๊ฐ์ฒด ์์ฑ ์ ๊ธฐ๋ณธ ์์ฑ์ ์ฌ์ฉ
- @Getter: Lombok๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก Getter ๋ฉ์๋ ์์ฑ
- Setter๋ฅผ ์ฐ์ง ์๋ ์ด์ : ์ด๋ค ํฌ์ธํธ์์๋ ์ง Setter ํจ์ ํธ์ถ์ ํตํด Entity์ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค๋ฉด, ๋ณ๊ฒฝํ ์๋๋ฅผ ํ์
ํ๊ธฐ ์ด๋ ค์ → ์๋๋ฅผ ํ์
ํ ์ ์๋ ํจ์๋ฅผ ๋ง๋ค์ด ํธ์ถ
( More: https://yoonbing9.tistory.com/28 )
- Setter๋ฅผ ์ฐ์ง ์๋ ์ด์ : ์ด๋ค ํฌ์ธํธ์์๋ ์ง Setter ํจ์ ํธ์ถ์ ํตํด Entity์ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค๋ฉด, ๋ณ๊ฒฝํ ์๋๋ฅผ ํ์
ํ๊ธฐ ์ด๋ ค์ → ์๋๋ฅผ ํ์
ํ ์ ์๋ ํจ์๋ฅผ ๋ง๋ค์ด ํธ์ถ
- @Id: ๊ธฐ๋ณธํค ๋งคํ
- @GeneratedValue์ ๊ฐ์ด ์ฌ์ฉ ์, ์๋ ์์ฑ
- @Builder: ์ํฐํฐ ๊ฐ์ฒด ์์ฑ์ → Builder ํจํด ์ ์ฉ
- ์ธ์๊ฐ ๋ง์ ๊ฒฝ์ฐ ์์ ํ๊ฒ ๊ฐ์ฒด ์์ฑ ๊ฐ๋ฅ
- ์ธ์์ ์์์ ์๊ด์์ด ๊ฐ์ฒด ์์ฑ ๊ฐ๋ฅ
[ ์ฐ๊ด๊ด๊ณ ๋งคํ ]
Todo๋ Group๊ณผ ๋จ๋ฐฉํฅ N:1 ๊ด๊ณ๋ฅผ ๊ฐ์ง๊ณ ์์
- ManyToOne
- ๋ค๋์ผ ๊ด๊ณ๋ฅผ ๋ํ๋ด๋ ๋งคํ์ ๋ณด
- fetch = fetchType.LAZY
- ToDo๋ฅผ ์กฐํํ ์, Group์ ์กฐํํ๋ ์ฟผ๋ฆฌ๊ฐ ์์ฑ๋์ง ์๊ณ , ์ค์ ๋ก Group์ ์ฌ์ฉํ๋ ์์ ์์ Group์ ์กฐํํ๋ ์ฟผ๋ฆฌ๊ฐ ๋๊ฐ → Proxy๋ก ์กฐํ
- JoinColumn(name=”team_seq”)
- ๋งคํํ ์ธ๋ํค์ ์ด๋ฆ์ ๋ช ์ํ์ฌ ์์ฑ
- More: https://private-space.tistory.com/87
2. Repository ์์ฑ
public interface TodoRepository extends JpaRepository<Todo, Long> {}
JPARepository๋ฅผ ์์ ๋ฐ๋ interface์์ฑ
- JPARepository
- CRUD ๋ฉ์๋๋ฅผ ์ ๊ณต
- ๋ฉ์๋ ๋ช ๋ช ๊ท์น์ ๋ฐ๋ผ ๋ฉ์๋ ์ด๋ฆ์ ์ง์ด์ฃผ๋ฉด, JPA๊ฐ ์ด๋ฆ์ ๋ถ์ํด ์ ์ ํ JPQL ์คํ
3. Repository ๋จ์ํ ์คํธ
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class TodoRepositoryTest {
@Autowired
private TodoRepository todoRepository;
@Test
void testCreateTodo() {
// given
Todo todo = Todo.builder()
.name("ํ ์ผ")
.content("๋ด์ฉ")
.predictedPomo(1)
.realPomo(1)
.status(TodoStatus.TODO)
.workImportance(WorkImportance.LOWER)
.workProficiency(WorkProficiency.ADVANCED)
.build();
// when
Todo savedTodo = todoRepository.save(todo);
// then
assertThat(savedTodo.getName()).isEqualTo("ํ ์ผ");
assertThat(savedTodo.getContent()).isEqualTo("๋ด์ฉ");
assertThat(savedTodo.getPredictedPomo()).isEqualTo(1);
assertThat(savedTodo.getRealPomo()).isEqualTo(1);
assertThat(savedTodo.getStatus()).isEqualTo(TodoStatus.TODO);
assertThat(savedTodo.getWorkImportance()).isEqualTo(WorkImportance.LOWER);
assertThat(savedTodo.getWorkProficiency()).isEqualTo(WorkProficiency.ADVANCED);
}
}
๋ฐ์ํ
'๐ป ๊ฐ๋ฐ ์ผ์ง > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SpringBoot - Slack Bot ์ฐ๋ํ๊ธฐ (0) | 2022.04.05 |
---|---|
[Error] org.mockito.exceptions.misusing.UnnecessaryStubbingException: Unnecessary stubbings detected. (0) | 2022.03.27 |
[Test] SpringBoot Test - Service (Mockito) (0) | 2022.03.27 |
[22.03.09] QueryDSL (0) | 2022.03.09 |
[22.03.04] Service, Controller ์์ฑ (0) | 2022.03.07 |