2021 08 23 학습로그 데모데이4 2021-08-23 학습로그 데모데이 4¶ 자기참조 테이블¶ 자기 참조 관계 - 하나의 엔티티가 다른 엔티티가 아니니 자기 자신과 관계를 맺는 타입 - 동일 엔티티 타입 내에서 자기가 자기의 인스턴스를 참조하는 구조 - 계층 구조를 나타내기에 유용 - 놀토에서는 댓글-대댓글에 사용 - FK로 연결하지 않고 상위 계층의 id는 자기 참조를 통해 나타냄 링크 - https://github.com/PapimonLikelion/woowacourse-TIL/blob/master/Level3/2021-08-03.md JPA N+1 문제¶ 발생하는 경우 - fetchType.EAGER + findAll() 메서드 호출하는 경우 N+1 개 만큼 쿼리가 나감 - fetchType.LAZY + Loop으로 조회하는 경우 N+1 개 만큼 쿼리가 나감 해결 방법 - JPQL 조인 페치 사용 - 어떤 자식 엔티티까지 join해서 가져올지 명시 링크 - https://github.com/PapimonLikelion/woowacourse-TIL/blob/master/Level3/2021-08-03.md Spring EventListener & TransactionalEventListener¶ 왜 필요하지? - 복잡한 도메인 의존성을 줄이기 위함 - 직접적으로 연관이 있는 로직이 아님에도 빈을 주입받아 호출하는 것은 너무 강력한 연관관계를 맺어준 것 - ex. 댓글/좋아요가 달렸을 때 알림을 보내주는 것 EventListener - 호출 즉시 로직 시작 TransactionalEventListener - 해당 트랜잭션이 끝나면 로직 시작 링크 - https://github.com/PapimonLikelion/woowacourse-TIL/blob/master/Level3/2021-08-04.md - https://jobc.tistory.com/200 - https://javacan.tistory.com/entry/Handle-DomainEvent-with-Spring-ApplicationEventPublisher-EventListener-TransactionalEventListener - https://engkimbs.tistory.com/827 - https://reiphiel.tistory.com/entry/understanding-of-spring-transaction-management-practice