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번의 제목이 가져와짐

'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 |