[JPA] 그 유명한 1:N관계
예
예준천
queryfactory.select(evaluation)
.from(evaluation)
.where( 비즈니스조건)queryfactory.select(evaluation)
.from(evaluation)
.join(evaluation.category, category).fetchJoin()
.join(category.user, user).fetchJoin()
.where( 비즈니스조건 )List<Category> ctgrs = queryFactory.select(category)
.from(category)
.where();
List<Long> ctgrsId = ctgrs.stream.map(c -> c.id).collect(Collectors.toList());
List<Question> questions = queryFactory.select(question)
.from(question)
.where(question.ctgrId.in(ctgrsId)));
// map 어쩌구 저쩌구
List<Category> ctgrs = queryFactory.select(category)
.from(category)
.where();
List<Question> questions = queryFactory.select(question)
.from(question)
.where(question.ctgr.in(ctgrs)));
Map<Long, List<Question>> categorizedQuestion = questions.stream
.collect(Collectors.groupingBy(q -> q.getCategory().getId);
List<FormDto> response = ctgrs.stream().map(c -> FormDto.from(c, categorizedQuestion.get(c.getId()))).collect(Collectors.toList());
@Service
public class EvaluationService {
@Autowired
private EvaluationRepository evaluationRepository;
public List<CategoryDTO> getEvaluationDetails(Long evaluationId) {
Evaluation evaluation = evaluationRepository.findByIdWithDetails(evaluationId);
List<CategoryDTO> categoryDTOs = new ArrayList<>();
for (Category category : evaluation.getCategories()) {
CategoryDTO categoryDTO = new CategoryDTO();
categoryDTO.set분류(category.getName());
List<QuestionDTO> questionDTOs = new ArrayList<>();
for (Question question : category.getQuestions()) {
QuestionDTO questionDTO = new QuestionDTO();
questionDTO.set문항이름(question.getName());
questionDTO.set배점(question.getScore());
List<AnswerLineDTO> answerLineDTOs = new ArrayList<>();
for (AnswerLine answerLine : question.getAnswerLines()) {
AnswerLineDTO answerLineDTO = new AnswerLineDTO();
answerLineDTO.set유저(answerLine.getUser().getName());
answerLineDTO.set코멘트(answerLine.getComment());
List<VendorScoreDTO> vendorScoreDTOs = new ArrayList<>();
for (Answer answer : answerLine.getAnswers()) {
VendorScoreDTO vendorScoreDTO = new VendorScoreDTO();
vendorScoreDTO.set업체명(answer.getVendor().getName());
vendorScoreDTO.set점수(answer.getScore());
vendorScoreDTOs.add(vendorScoreDTO);
}
answerLineDTO.set업체별점수(vendorScoreDTOs);
answerLineDTOs.add(answerLineDTO);
}
questionDTO.set응답라인(answerLineDTOs);
questionDTOs.add(questionDTO);
}
categoryDTO.set문항(questionDTOs);
categoryDTOs.add(categoryDTO);
}
return categoryDTOs;
}
}@Mapper(componentModel = "spring")
public interface EvaluationMapper {
CategoryDTO toCategoryDTO(Category category);
}
... service code
public List<CategoryDTO> getEvaluationDetails(Long evaluationId) {
Evaluation evaluation = evaluationRepository.findByIdWithDetails(evaluationId);
// 엔티티를 DTO로 변환
return evaluation.getCategories().stream()
.map(evaluationMapper::toCategoryDTO)
.collect(Collectors.toList());
}>> 원하는 요소 get**() 으로 작성
public interface UserInfoMapping {
String getId();
String getUsername();
Whatever getDepartment();
interface Whatever{
String getTeamName();
}
}
public interface PersonRepository extends Repository<Person, Long> {
<T> T findByLastName(String lastName, Class<T> type);
}