BACKEND/SpringBoot
답변 AnswerRepository 생성 및 데이터 저장 ,검색
죠으닝
2023. 11. 8. 11:03
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번의 제목이 가져와짐