[01.24] 내일배움캠프[Spring] WIL-11

박상훈·2023년 1월 24일
0

내일배움캠프[WIL]

목록 보기
11/12

[01.24] 내일배움캠프[Spring] WIL-11

1. 지난 일주일

  • Market 매칭 시스템을 구현하는 간단한 프로젝트를 진행했다.
  • 내가 맡은 기능 : Customer, Seller , Searching

2. 어려웠던 점

  • 우리 프로젝트는 연관관계 @ManyToOne, @OneToMany등을 사용하지 않고 ID매핑으로만 구성했다.
  • 따라서 만약 회원의 이름으로 상품을 조회한다 -> 결과 : 이름 ,상품명 ... 이 나와야한다.
  • 하지만 Product Entity에는 username의 컬럼이 없다.
    -> Product Entity에 username도 추가? -> 상품테이블인데 유저 이름을 가지고 있는 것이 맞아..?
    -> 결국 join쿼리를 통해 User Entity + Product Entity하여 Searching을 진행해야 한다는 결론에 도달
 @Query("select new com.sparta.morningworkout.dto.search.ProductResponseSearchByNameDto(p.productName,p.price,u.username,p.category)" +
            " from users u LEFT join Product p on u.id = p.userId where u.role= 'SELLER' AND u.username like %:sellerName%")
    Page<ProductResponseSearchByNameDto> findAllBySellerName(String sellerName, Pageable pageable);

    @Query("select new com.sparta.morningworkout.dto.product.ProductResponseDto(p.productName,p.price,p.category) from Product p where p.userId=:sellerId AND p.productName like %:keyword%")
    Page<ProductResponseDto> findAllBySellerIdByProductName(String keyword, long sellerId, Pageable pageable);
    
     @Query("select new com.sparta.morningworkout.dto.admin.UserContentsResponseDto(u.username, p.nickname) " +
            "from users u left join Profile p on u.id = p.id where u.role = :role")
    Page<UserContentsResponseDto> findAllByRoleOrderByIdDescQuery(@Param("role") UserRoleEnum role, Pageable pageable);
    
    @Query("select new com.sparta.morningworkout.dto.admin.UserContentsResponseDto(u.username, p.nickname) " +
            "from users u left join Profile p on u.id = p.id where u.role = 'CUSTOMER' and p.nickname " +
            "like %:keyword%")
    Page<UserContentsResponseDto> findCustomersByProfileNicknameKeyword(Pageable pageable, @Param("keyword")String keyword);


    @Query("select new com.sparta.morningworkout.dto.admin.UserContentsResponseDto(u.username, p.nickname) " +
            "from users u left join Profile p on u.id = p.id where u.role = 'SELLER' and p.nickname " +
            "like %:keyword%")
    Page<UserContentsResponseDto> findSellersByNickname(Pageable pageable, @Param("keyword")String keyword);

3. 배운점

  • jpa가 어떤 키워드를 포함하는 쿼리를 자동으로 만들어서 사용하고 싶다? -> Containing
  • 내가 아는 OuterJoin은 PK < - > FK 가 일치하지 않아도 LEFT , RIGHT기준으로 정보를 전부 가져오는 것으로 알고있다.
  • 하지만 Where 조건을 걸어서 그런지 일치하는 정보만 서칭해왔다.
  • JPQL의 형식은 SQL과 조금 다르다.. -> 조금 더 공부해 볼 것!

4. 느낀점

  • Join쿼리를 직접사용하여 동작을 확인했을 때 희열감..!
  • 연관관계는 굳이 필요가 없다..! -> ID매핑으로도 충분히 다 잘됨
    -> 어차피 연관관계 설정해도 ID값으로 저장되는 필드 1개를 차지하는데, ID매핑도 괜찮은 듯..!
  • 설계 부분을 많이 참여하지 못해 아직 설계가 어렵다..!
profile
기록하는 습관

0개의 댓글