인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

dohyun00471067's profile image
dohyun00471067

asked

Real-world! Spring Boot and JPA Utilization 2 - API Development and Performance Optimization

Order Lookup V6: Direct Lookup from JPA to DTO, Flat Data Optimization

gropingBy 에 관하여입니다.

Written on

·

468

5

     Set<Map.Entry<OrderQueryDto, List<OrderItemQueryDto>>> entries = flats.stream()
                .collect(groupingBy(o ->  new OrderQueryDto(o.getOrderId(), o.getName(),
                                o.getOrderDate(), o.getOrderStatus(), o.getAddress()),
                        mapping(o -> new OrderItemQueryDto(o.getOrderId(),
                                o.getItemName(), o.getOrderPrice(), o.getCount()), toList())))
                .entrySet();

여기서 OrderQueryDto 와 Map 의 키(groupingby 의 classifier)가  되는 것 같은데 아무리 구글링해봐도 객체인경우에는  @EqualsAndHashCode를 통해 여러 필드중 특정 필드를 지정해줘야한다? 이런말을 못 찾겠습니다...

질문도 좀 이상한것 같은데 제가 궁금한것은 어떤 키워드나 사이트를 참조해서 공부해야하는지 입니다...

찾아본 예시들도 다 그냥 일반적인 경우라
어떻게 equalshashcode를 알려줘야하는지  원천? 을 알고싶습니다.

spring-bootspringjavaJPA

Answer 2

1

dohyun00471067님의 프로필 이미지
dohyun00471067
Questioner

늦은시간 감사합니다! Map 이 키가 같은지 비교할때 hash(key)를 비교하는데 

@EqualsAndHashCode of("orderid") 를 해주면 orderid로만 equals 함수와 hash함수를 오버라이딩 해줘서

mapping이 루프를 돌때 key가 같은것으로 인식하여서 orderItems가 처음처럼 따로 안생기고 같이 묶인다고 이해하면 맞나요?

yh님의 프로필 이미지
yh
Instructor

네 맞습니다^^

1

yh님의 프로필 이미지
yh
Instructor

안녕하세요. 도현님

다음 블로그에서 2.3. 복잡한 맵 키 유형으로 그룹화 부분을 보시면 도움이 되실거에요.

https://recordsoflife.tistory.com/55

또는 여기도요. https://gem1n1.tistory.com/159

감사합니다.

dohyun00471067's profile image
dohyun00471067

asked

Ask a question