728x90
Question๊ณผ ๋์ผํ๊ฒ
Answer๋ Repository ๋ง๋ ๋ค.

package com.mysite.sbb;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AnswerRepository extends JpaRepository<Answer, Integer> {
}


//์ผ๋จ ์ง๋ฌธQuestion ๊ฐ์ ธ์์ผํจ ํด๋น ์ง๋ฌธ์ ๋ํ ๋ต๋ณ์ด๋๊น
Optional<Question> oq = this.qRepo.findById(2);
Question q = oq.get();
Answer a = new Answer();
a.setContent("๋ค ์๋์ผ๋ก ์์ฑ๋ฉ๋๋ค.");
a.setQuestion(q);
a.setCreateDate(LocalDateTime.now());
aRepo.save(a);


//๋ต๋ณ ์กฐํํ๊ธฐ
Optional<Answer> oa = this.aRepo.findById(1);
Answer a = oa.get();
System.out.println(a.getQuestion().getSubject()); //a๋ต๋ณ์ ํด๋นํ๋ ์ง๋ฌธ์ ์กฐํํ์ฌ, ์ ๋ชฉ์ ๊ฐ์ ธ์จ๋ค.

1๋ฒ ๋ต๋ณ์ด ๋ฌ๋ฆฐ ์ง๋ฌธ์ 2๋ฒ ์ง๋ฌธ์ ๊ทธ๋์ 2๋ฒ์ ์ ๋ชฉ์ด ๊ฐ์ ธ์์ง

728x90
'BACKEND > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Question ์ปจํธ๋กค๋ฌ ์์ฑ (1) | 2023.11.08 |
|---|---|
| ํจํค์ง ๋๋ฉ์ธ ๋ณ ๋ถ๋ฆฌํ๊ธฐ (0) | 2023.11.08 |
| Repository , JUnit ํ ์คํธ (1) | 2023.11.08 |
| JPA์ ๊ฐ๋ ๋ฐ H2๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ (0) | 2023.11.07 |
| ์คํ๋ง๋ถํธ ํ๋ก์ ํธ์ ๊ตฌ์กฐ, port๋ฒํธ ์ค์ ๋ฐ context-path ์ค์ (1) | 2023.11.07 |