Skip to content
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

Spring Data JPA 와 테크닉 #17

Merged
merged 5 commits into from
Sep 26, 2021
Merged

Spring Data JPA 와 테크닉 #17

merged 5 commits into from
Sep 26, 2021

Commits on Sep 26, 2021

  1. Change #13 - test DB from H2 to MySQL

    mysql 적용해보기
    아직은 테스트 단계
    
    * username, password 관리는 추후 고민하기
    * 지금은 테스트에서도 mysql 을 사용하게 하기
      * 동작 관찰을 위함
      * 이후엔 서비스는 mysql로, 테스트는 h2로 하게끔
      * 따라서 h2 디펜던시를 아예 제거하진 않음
    * `spring.h2.console.enabled=false` 명시
      * 기본값 `false`지만, devtools auto config 이 h2-console을 작동시킴
    * `spring.sql.init.mode=always`: 연습을 위해 서비스 동작에서도 `data.sql`의 테스트 데이터를 반영. 추후 실제 프로덕션이 동작할 땐 제거를 검토
    * ddl-auto: `create` - 테스트 실행 후 스키마를 날리지 않고 mysql 안을 관찰하기 위함
    djkeh committed Sep 26, 2021
    Configuration menu
    Copy the full SHA
    a8d5b4f View commit details
    Browse the repository at this point in the history
  2. Refactor #13 - entities with @EqualsAndHashCode

    엔티티의 동등성 비교를 편하게 하려고 롬복을 도입했는데
    엔티티는 자바의 데이터 클래스랑은 다른 특성을 이용해
    이 동등성 기준을 각 필드 전체 비교에서 `id` 비교로 하고자 함
    즉 `a(id=1)`, `b(id=1)` 일 때,
    `a.equals(b) == True`
    
    그러나 id는 persistence context가 관리하므로
    서비스 코드에서 처음 만들 땐 `null` 인데,
    롬복이나 ide 자동 작성 코드는 이 부분을 `true`로 처리함
    즉 `a(id=null)`, `b(id=null)` 일 때,
    `a.equals(b) == True`
    
    그냥 보면 맞는 것 같지만 실은 둘이 단지 아직 `id`를
    부여받지 못했을 뿐, 다른 데이터를 가진
    서로 다른 엔티티일 수 있기 때문에,
    `equals()` 직접 구현이 필요.
    
    한편 `hashCode()`도 구현해줘야 하는데
    동등성 기준을 `id`로 잡는다면 `hashCode()` 기준값도
    `id`를 이용해 다양한 수를 만들면 좋지만
    위와 같은 이유로 영속화가 안된 엔티티는 아직 `id`가 없으므로
    그냥 상수를 넣든지, 다른 방법이 필요함
    
    상수를 넣어도 큰 무리 없지만 좀 더 효율적으로 만들기 위해
    각 엔티티 인덱스 컬럼을 활용해 `hash()` 생성
    djkeh committed Sep 26, 2021
    Configuration menu
    Copy the full SHA
    66c8c52 View commit details
    Browse the repository at this point in the history
  3. Update #13 - entity relations using @ManyToOne, @OneToMany

    JPA 를 본격적으로 시작하면서 엔티티 연관관계를
    `id` 참조에서 object 참조로 개선
    이에 맞춰 대응하는 테스트와 dto도 수정
    
    `@ManyToOne`은 foreign_key 를 생성하므로
    기존 인덱스 지정은 불필요
    순서를 주기 위해 자료구조는 `LinkedHashSet`,
    애노테이션 `OrderBy` 추가
    
    ### TODO
    
    spring data rest api 에서는 검색어를 맞춰 수정했는데
    검색 파라미터가 `place.placeName`이 되어 예쁘지 않음.
    개선안은 추후 생각해 보기로.
    
    ### Reference
    
    * `@OneToMany` hibernate type: `OrderedSetType`
    djkeh committed Sep 26, 2021
    Configuration menu
    Copy the full SHA
    154a0c3 View commit details
    Browse the repository at this point in the history
  4. Refactor #13 - entity annotations

    `@OneToMany`에 빠진 정렬 애노테이션 추가
    애노테이션 작성 줄 정리
    djkeh committed Sep 26, 2021
    Configuration menu
    Copy the full SHA
    e6fc4b8 View commit details
    Browse the repository at this point in the history
  5. Update #13 - place information in dto

    API 스펙이 바뀌어 `placeId` -> `place`를 내어줌에 따라
    dto 스펙도 변경.
    이걸로 이벤트 화면에서 쉽게 장소 데이터를 표현 가능
    djkeh committed Sep 26, 2021
    Configuration menu
    Copy the full SHA
    fb6c11e View commit details
    Browse the repository at this point in the history