JPA

모르는개산책·2024년 5월 16일
  1. Jpa 설정

    • dependency 설정(build.gradle.kts)
      implementation(libs.spring.boot.starter.data.jpa)
    • applicatioin.yml 설정
      spring:
        ## JPA Setting Info
        jpa:
          hibernate:
            ddl-auto: create  # option type: create, create-drop, update, validate, none(아래 표 참고)
          properties:
            hibernate:
              diarect: org.hibernate.dialect.H2Dialect  # 쿼리의 기준이 되는 데이터베이스 엔진을 설정합니다.
              show_sql: true  # sql 쿼리를 보여줍니다.
              format_sql: true  # sql query formatting
              use_sql_comments: true  # sql 쿼리의 추가정보를 보여줍니다.
      
      logging:
        level:
          org:
            hibernate:
              type:
                descriptor:
                  sql: trace  # query의 ?에 어떤 값이 들어가는지 추적할 수 있는 설정입니다. TRACE Log에 값을 보여줍니다.
      스크린샷 2024-05-13 오전 9.37.36.png
  2. Entity class 생성 (설정대로 테이블이 생성되므로 정확한 설정 필요)

    • @Entity
    • @Table(name=tableName) : 테이블 지정
    • @Id : PK field 지정
    • @GeneratedValue(strategy = GenerationType.IDENTITY)
      : AUTO_INCREMENT 설정 (id값이 null일 경우 자동 생성)
    • @Column(unique=true…) : column에 대한 속성 지정
      그 외 DB 관계에 따라 @ManyToOne, @JoinColumn 설정
  3. Repository interface 생성

    • JpaRepository<T,T> 을 상속받아 필요 메소드 작성
    • 구현체 필요X
  4. Service 에서 Repository interface 주입받아 사용

    • JpaRepository 기본 메소드

참고 :
https://dbjh.tistory.com/77
https://ccomccomhan.tistory.com/131
https://herojoon-dev.tistory.com/135
https://frogand.tistory.com/22

0개의 댓글