-
Notifications
You must be signed in to change notification settings - Fork 0
feat : [JPA 활용 미션 ] 5주차 과제 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.example.umc_9th.domain.member.repository; | ||
|
|
||
| import com.example.umc_9th.domain.member.entity.Member; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface MemberRepository extends JpaRepository<Member, Long> { | ||
|
|
||
|
|
||
| //2 id 로 멤버 테이블 조회 | ||
| //마이 페이지 화면 쿼리 | ||
| //(메서드 생성 방식 권장) | ||
| Optional<Member> findById(Long id); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JpaRepository에 기본 내장되어 있습니다. |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.example.umc_9th.domain.mission.repository; | ||
|
|
||
| import com.example.umc_9th.domain.mission.entity.Mission; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| public interface RegionMissionRepository extends JpaRepository<Mission,Long> { | ||
|
|
||
|
|
||
| //4. 홈 화면 쿼리 | ||
| //(현재 선택 된 지역에서 도전이 가능한 미션 목록, 페이징 포함) | ||
|
|
||
|
|
||
| //1. 미션 테이블 통해서 가게 테이블 -> 지역 테이블 -> 세부지역 테이블 미션이 해당 | ||
|
|
||
| // WHERE r.id = :regionId 는 자리표시자로 값 비교 | ||
| @Query(value = """ | ||
| SELECT m FROM Mission m | ||
| JOIN FETCH m.store s | ||
| JOIN FETCH s.region r | ||
| JOIN FETCH r.subRegion sr | ||
| WHERE r.id = :regionId | ||
| """, | ||
| countQuery = "SELECT count(m) FROM Mission m JOIN m.store s WHERE s.region.id = :regionId") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. countQuery -> 이게 뭔가 해서 알아봤는데, fetchJoin + Page 하면 CountQuery를 정상적으로 만들지 못한다는걸 배웠네요. 굿굿 |
||
| Page<Mission> findMissionsByRegion( | ||
| @Param("regionId") Long regionId, | ||
| Pageable pageable | ||
| ); | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.example.umc_9th.domain.mission.repository; | ||
|
|
||
| import com.example.umc_9th.domain.mission.mapping.UserMission; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| public interface UserMissionRepository extends JpaRepository<UserMission, Long> { | ||
|
|
||
|
|
||
| //3.내가 진행중, 진행 완료한 미션 모아서 보는 쿼리(페이징 포함) | ||
|
|
||
| // join fetch : um과 연관된 mission 엔티티 정보까지 가져옴 (N+1) 문제 해결합니다 | ||
| @Query(value="SELECT um from UserMission um join fetch um.mission m"+ | ||
| // um의 멤버필드에 있는 id가 memgerId이름의 필드를 가져옴 | ||
| " where um.member.memberId=:memberId", | ||
| // 페이징 처리를 위해 전체 데이터가 몇 개인지 계산하는 쿼리 지정합니다 | ||
| countQuery = " SELECT count(um) FROM UserMission um WHERE um.member.memberId = :memberId") | ||
| Page<UserMission>findMyMission(@Param("memberId") Long memberId, Pageable pageable); | ||
|
|
||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.example.umc_9th.domain.review.repository; | ||
|
|
||
| import com.example.umc_9th.domain.member.entity.Member; | ||
| import com.example.umc_9th.domain.review.entity.Review; | ||
| import com.example.umc_9th.domain.store.Store; | ||
| import org.springframework.data.jpa.repository.EntityGraph; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface ReviewRepository extends JpaRepository<Review, Long> { | ||
|
|
||
|
|
||
|
|
||
| //2. 리뷰 작성하는 쿼리, | ||
| //* 사진의 경우는 일단 배제 | ||
| //(메서드 생성 방식 권장) | ||
| // id로 리뷰 조횔 할 떄 member도 같이 조회 | ||
| // 리뷰 조회할 떄 member 엔티티도 같이 가져오게 하는 에노테이션 = @EntityGraph | ||
| @EntityGraph(attributePaths = {"member"}) | ||
| Optional<Review> findById(Long id); | ||
| // Member와 Store를 기준으로 Review 조회 | ||
| boolean existsByMemberAndStore(Member member, Store store); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ spring: | |
| database-platform: org.hibernate.dialect.MySQLDialect # | ||
| show-sql: true # | ||
| hibernate: | ||
| ddl-auto: create # | ||
| ddl-auto: create-drop # | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 실제로 프젝할 때는 이걸루 두면 안돼요..ㅎ |
||
| properties: | ||
| hibernate: | ||
| format_sql: true # | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private Long id; -> 이렇게 해도 member_id로 컬럼 이름 저장됩니다.
private Long memberId => 이렇게 하면 CammelCase -> snake_case로 자동 변환돼서 member_id로 저장됩니다.
∴ 그냥 id로 저장해도 된다~