springframework/Spring Data JPA

Value 타입 맵핑

Jungsoomin :) 2020. 11. 13. 18:10

Entity 타입이란

  • 고유한 식별자를 가진다.
  • 다른 Entity 에서 독립적으로 참조한다.

Value 타입이란

  • Entity 를 통해 참조되는 타입을 말한다.
  • Entity 에 종속적이다.

Composite Value 타입

  • 기본 Value 타입보다 포괄적인 범위의 Value 타입이다.
  •  Composite Value 타입의 멤버변수 또한 @Column 이 생략되어 있는 것과 같다.
  • Composite Value 타입을 선언할 때에는 @Embeddable 을 사용한다.
  • Composite Value 타입을 사용할 때에는 @Embedded 을 사용한다.
  • 하나의 Composite Value 타입을 재정의 할 때에는 
  • @AttributeOverrides 를 사용하며 @AttributeOverride 의 배열을 사용하여 기존 Composite Value 타입의 멤버 변수를 다른 이름으로 재정의한다.

Composite Value = @Embeddable

@Embeddable
public class Address {

    @Column
    private String street;

    private String city;

    private String state;

    private String zipCode;

}

Entity 타입에서의 Composite Value 사용 = @Embedded

Composite Value 타입의 컬럼명 재정의 = @AttributeOverrides ({@AtrributeOverride})

@Entity
public class Account {

    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false, unique = true)
    private String username;

    @Column(columnDefinition = "varchar(255) not null")
    private String password;

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


    private String yes;

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

    @Transient
    private String no;

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        Account account = (Account) o;
        return id.equals(account.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Composite Value 타입의 재정의 없이 사용 = 해당 타입의 멤버변수들이 컬럼에 추가 된 모습

Hibernate: 
    
    create table account (
       id int8 not null,
        city varchar(255),
        state varchar(255),
        street varchar(255),
        zip_code varchar(255),
        created time,
        password varchar(255) not null,
        username varchar(255) not null,
        yes varchar(255),
        primary key (id)
    )

Compositve Value 타입의 재정의를 한 모습 = 해당 멤버변수의 컬럼명이 변경된 모습

Hibernate: 
    
    create table account (
       id int8 not null,
        city varchar(255),
        state varchar(255),
        home_street varchar(255),
        zip_code varchar(255),
        created time,
        password varchar(255) not null,
        username varchar(255) not null,
        yes varchar(255),
        primary key (id)
    )

 

가만히 들여다보기에, 기본 타입의 재사용성이 굉장히 높다고 생각이 듬.

'springframework > Spring Data JPA' 카테고리의 다른 글

Cascade, Entity 상태  (0) 2020.11.13
관계 맵핑, 1 : N  (0) 2020.11.13
엔티티타입 맵핑  (0) 2020.11.13
JPA 실행하고 적용하기  (0) 2020.11.09
ORM 패러다임 불일치  (0) 2020.11.08