[Spring] Spring JPA 시작하기(기본 설정 값, JPA 객체 생성)

2 minute read

Spring JPA 설정 따라하기

  1. [Spring] Spring JPA 시작하기(기본 설정 값, JPA 객체 생성)

  2. [Spring] 다중 데이터소스 설정(Multiple Datasource JPA, Mybatis)

  3. [Spring] application.yaml 설정하기 (다중 설정, custom.yaml 생성)

  4. [Spring] Controller와 Service 생성하기

  5. [Spring] 간단한 TestCase 만들기

  6. [Spring] Querydsl 설정하기

현재까지는 스프링 부트를 사용하면서 DB 관리를 위하여 마이바티스를 사용하고 있었다.

그러나 반복적인 코드 작성으로 인한 시간 소모가 생각보다 크다는 것을 알게 되었고 이를 소폭이라도 감소시키기 위하여 JPA를 사용하기로 하였다.

역시나 스프링이 지원해주는 스프링 JPA를 사용하여 빠른 개발을 해보자.

JPA 준비

의존성 추가(build.gradle)

// 스프링MVC를 위한 Spring boot starter 패키지 
implementation 'org.springframework.boot:spring-boot-starter-web'

// 스프링JPA를 위한 패키지
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.2.1.RELEASE'

// RDBMS는 Postgresql 사용
runtimeOnly 'org.postgresql:postgresql'

// 테스트 환경을 위한 패키지
testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

설정추가 (application.yaml)

spring:
  datasource:
      password: pw123
      jdbc-url: jdbc:postgresql://localhost:5432/testdb123
      username: user123

  jpa:
    hibernate:
      ddl-auto: none
    database: postgresql
    show-sql: true
    properties:
      hibernate:
        format_sql: true

JPA의 구현체인 Hibernate를 사용하였다.

간략하게 해석을 하자면 다음의 내용이다.

datasource :

  • DB는 Postgres에 계정 ID, PW, Database는 각각 user123, pw123, testdb123 으로 지정하였다.

jpa :

  • jpa 설정으로 DDL(CREATE, DROP TABLE)을 자동으로 진행 시 마다 none(아무것도)하지 않겠다.
  • database는 postgresql을 사용하며 jpa로 발생한 쿼리에 대해서 sql문을 보겠다.
  • 보여지는 sql문에 대한 설정으로 format이 유지된 sql을 보겠다.

여기서 잠깐! JPA와 Hibernate, Spring data JPA에 대한 차이점을 모른다면 다음의 포스팅을 참고하자.

[Spring] 헷갈리는 용어 JPA, Hibernate, Spring data JPA 차이점

JPA에서 사용될 도메인 객체를 생성하자.

@Entity(name = "account")
public class Account {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "user_name", insertable = false, updatable = false, nullable = false, unique = true)
    private String username;

    private String password;

    private String email;

    @Transient
    private String testValue;        

    @Temporal(TemporalType.TIMESTAMP)
    private Date created = new Date();

    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name="street", column = @Column(name="home_street"))
    })
    private Address address;


    getter, setter 생략... 
}

@Entity : Spring의 persistance context를 통해 관리할 것임을 밝힌다.

  • JPA가 관리하는 클래스로 해당 어노테이션 붙은 경우에 해당 클래스를 JPA에 매핑할 객체로서 관리하게 된다.

[속성]

@Entity(name= “테이블명”) 객체와 테이블 매핑 시 클래스명을 기본 값으로 채택하나, 테이블명이 다를 시 별도 지정해주어야 한다.

[주의]

  • 기본 생성자 필수

    파라미터가 없는 public 또는 protected 생성자가 필요하다.

    JPA spec으로 규정되어 있다.

    Why? JPA를 구현해서 쓰는 라이브러리들이 다양한 기술(Ex. Reflection)을 사용해서 객체를 프록싱할 때 필요하기 때문.

  • final 클래스, enum, interface, inner 클래스는 엔티티로 사용할 수 없다.

  • DB에 저장하고 싶은 필드에는 final을 사용할 수 없다.


@Id :

  • 해당 변수를 PK값으로 잡는다.

@GeneratedValue :

  • Id값에 해당하는 pk값을 자동으로 생성한다.

@Column :

  • Column을 매핑한다.

[속성]

  • @Column(name = “name”) 객체명과 DB 컬럼명을 다르게 하고 싶은 경우, DB 컬럼명으로 설정할 이름을 name 속성으로 적는다.
  • @Column(insertable = false, updatable = false) 컬럼을 수정했을 때 DB에 추가를 할 것인지 여부
  • @Column(nullable = false, unique = true) 해당 변수 값 null, unique 여부

@Transient :

  • 특정 필드를 데이터베이스 컬럼에 매핑하지 않음. 즉, 테이블과 관련 없는 변수임을 나타냄.
  • 해당 annotation이 붙은 필드는 DB에 저장, 조회가 되지 않는다.

@Temporal :

  • 시간, 날짜 타입을 지정하는 어노테이션

@Embedded :

  • Composite Value 타입을 지정할 때 쓰이는 어노테이션이며 이 Composite Value값은 자바의 클래스 형식으로 작성할 수 있다. @AttributeOverride 어노테이션을 통해서 Composite Value 타입의 멤버 변수를 테이블의 어떤 컬럼과 매칭할 지 커스터마이징할 수 있습니다.

package com.tutorial.springbootjpa;

import javax.persistence.Embeddable;

@Embeddable
class Address {

    private String street;

    private String city;

    private String state;

    private String zipCode;
}

@Embeddable :

  • 어노테이션을 통하여 이 클래스가 Composite Value를 나타내는 클래스이며 여기서 정의된 클래스는 위 엔티티의 Composite Value로서 사용될 수 있습니다.

references:

엔티티 매핑 방법 (Entity Mapping)

[Spring JPA #3] Entity 매핑 및 Value 타입 매핑

백기선님의 인프런 스프링 데이터 JPA

Leave a comment