Home > Spring > πŸƒ[Spring] JPA μ–΄λ…Έν…Œμ΄μ…˜ - `@GeneratedValue`

πŸƒ[Spring] JPA μ–΄λ…Έν…Œμ΄μ…˜ - `@GeneratedValue`
Spring Framework

πŸƒ[Spring] JPA μ–΄λ…Έν…Œμ΄μ…˜ - @GeneratedValue

  • @GeneratedValueλŠ” JPA(Java Persistence API)μ—μ„œ κΈ°λ³Έ ν‚€(Primary Key) 값을 μžλ™μœΌλ‘œ 생성할 λ•Œ μ‚¬μš©ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜μž…λ‹ˆλ‹€.
  • 이 μ–΄λ…Έν…Œμ΄μ…˜μ€ μ—”ν‹°ν‹°(Entity)의 κΈ°λ³Έ ν‚€(Primary Key) ν•„λ“œμ— 값을 μžλ™μœΌλ‘œ ν• λ‹Ήν•˜λŠ” 방식을 μ •μ˜ν•˜λ©°, λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ κΈ°λ³Έ ν‚€(Primary Key)κ°€ μƒμ„±λ˜λŠ” 방법을 μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

1️⃣ μ£Όμš” νŠΉμ§•.

1️⃣ μžλ™μœΌλ‘œ κΈ°λ³Έ ν‚€ κ°’ 생성.

  • @GeneratedValueλŠ” κΈ°λ³Έ ν‚€(Primary Key)에 μˆ˜λ™μœΌλ‘œ 값을 ν• λ‹Ήν•˜μ§€ μ•Šκ³ , λ°μ΄ν„°λ² μ΄μŠ€λ‚˜ JPA(Java Persistence API) κ΅¬ν˜„μ²΄κ°€ κΈ°λ³Έ ν‚€(Primary Key) 값을 μžλ™μœΌλ‘œ μƒμ„±ν•˜λ„λ‘ ν•©λ‹ˆλ‹€.

2️⃣ μ „λž΅(GenerationType) μ„€μ •.

  • @GenerationTypeλŠ” strategy 속성을 μ‚¬μš©ν•˜μ—¬, κΈ°λ³Έ ν‚€ κ°’(Primary Key)을 μƒμ„±ν•˜λŠ” 방식을 μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • JPA(Java Persistence API)λŠ” λ„€ 가지 생성 μ „λž΅μ„ μ œκ³΅ν•©λ‹ˆλ‹€.
    • AUTO
    • IDENTITY
    • SEQUENCE
    • TABLE

2️⃣ 생성 μ „λž΅(GenerationType)

1️⃣ GenerationType.AUTO

  • κΈ°λ³Έ ν‚€(Primary Key) 생성 μ „λž΅μ„ JPA(Java Persistence API) κ΅¬ν˜„μ²΄(예: Hibernate)κ°€ μžλ™μœΌλ‘œ μ„ νƒν•˜λ„λ‘ ν•©λ‹ˆλ‹€.
  • λ°μ΄ν„°λ² μ΄μŠ€μ— λ§žλŠ” 졜적의 방법을 JPA(Java Persistence API)κ°€ κ²°μ •ν•©λ‹ˆλ‹€.
    • 일뢀 λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œλŠ” SEQUENCE 방식, λ‹€λ₯Έ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œλŠ” IDENTITY 방식 등을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

2️⃣ GenerationType.IDENTITY

  • κΈ°λ³Έ ν‚€ 값이 μžλ™ μ¦κ°€ν•˜λŠ” 컬럼(Column,μ—΄)을 μ‚¬μš©ν•˜λŠ” λ°©μ‹μž…λ‹ˆλ‹€.
  • λ°μ΄ν„°λ² μ΄μŠ€κ°€ 직접 κΈ°λ³Έ ν‚€(Primary Key) 값을 μƒμ„±ν•©λ‹ˆλ‹€.
    • 예λ₯Ό λ“€μ–΄, MySQLμ—μ„œλŠ” AUTO_INCREMENT, SQL Serverμ—μ„œλŠ” IDENTITY 컬럼(Column, μ—΄)을 μ‚¬μš©ν•˜μ—¬ 값을 μžλ™μœΌλ‘œ μ¦κ°€μ‹œν‚΅λ‹ˆλ‹€.
  • IDENTITY μ „λž΅μ€ λ°μ΄ν„°λ² μ΄μŠ€μ— 의쑴적이며, μ¦‰κ°μ μœΌλ‘œ 값이 μƒμ„±λ©λ‹ˆλ‹€(데이터가 μ‚½μž…λ˜κΈ° 전에 미리 μ•Œ 수 μ—†μŒ).

3️⃣ GenerationType.SEQUENCE

  • μ‹œν€€μŠ€ 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ κΈ°λ³Έ ν‚€ 값을 μƒμ„±ν•©λ‹ˆλ‹€.
  • λ°μ΄ν„°λ² μ΄μŠ€μ˜ ν…Œμ΄λΈ”μ„ ν†΅ν•œ κ³ μœ ν•œ ID 값을 κ΄€λ¦¬ν•˜λ©°, 이 방식은 λ°μ΄ν„°λ² μ΄μŠ€ 독립적인 λ°©μ‹μ΄μ§€λ§Œ μ„±λŠ₯이 λ–¨μ–΄μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

3️⃣ @GeneratedValue μ‚¬μš© μ˜ˆμ‹œ.

1️⃣ AUTO μ „λž΅.

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

@Entity
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) // κΈ°λ³Έ ν‚€ 생성 μ „λž΅μ„ JPAκ°€ μžλ™μœΌλ‘œ 선택.
    private Long id;
    
    private String name;
    private String email;
    
    // κΈ°λ³Έ μƒμ„±μž 및 Getter, Setter
    public User() {}
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
}

2️⃣ IDENTITY μ „λž΅.

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

@Entity
public class Product {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) // λ°μ΄ν„°λ² μ΄μŠ€κ°€ μžλ™ μ¦κ°€ν•˜λŠ” λ°©μ‹μœΌλ‘œ κΈ°λ³Έ ν‚€(Primary Key) 생성
    private Long productId;
    
    private String name;
    private Double price;
    
    // κΈ°λ³Έ μƒμ„±μž 및 Getter, Setter
    public Product() {}
    
    public Long getProductId() {
        return productId;
    }
    
    public void setProductId(Long productId) {
        this.productId = productId;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Double getPrice() {
        return price;
    }
    
    public void setPrice(Double price) {
        this.price = price;
    }
}

3️⃣ SEQUENCE μ „λž΅.

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.SequenceGenerator;

@Entity
public class Order {
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "order_seq") // μ‹œν€€μŠ€
    @SequenceGenerator(name = "order_seq", sequenceName = "order_sequence", allocationSize = 1)
    private Long orderId;
    
    private String product;
    private int quantity;
    
    // κΈ°λ³Έ μƒμ„±μž 및 Getter, Setter
    public Order() {}
    
    public Long getOrderId() {
        return orderId;
    }
    
    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }
    
    public String getProduct() {
        return product;
    }
    
    public void setProduct(String product) {
        this.product = product;
    }
    
    public int getQuantity() {
        return quantity;
    }
    
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}
  • μœ„ μ½”λ“œμ—μ„œλŠ” @SequenceGeneratorλ₯Ό μ‚¬μš©ν•˜μ—¬ μ‹œν€€μŠ€μ— λŒ€ν•œ μ„ΈλΆ€ 섀정을 μ§€μ •ν•©λ‹ˆλ‹€.
    • sequenceName은 λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ μ‚¬μš©ν•  μ‹œν€€μŠ€μ˜ 이름을 μ •μ˜ν•˜κ³ , allocationSizeλŠ” μ‹œν€€μŠ€ 값을 미리 ν• λ‹Ήν•˜λŠ” 크기λ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.

4️⃣ μš”μ•½.

  • @GeneratedValueλŠ” JPA(Java Persistence API)μ—μ„œ κΈ°λ³Έ ν‚€(Primary Key)λ₯Ό μžλ™μœΌλ‘œ 생성할 λ•Œ μ‚¬μš©ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜μž…λ‹ˆλ‹€.
  • 생성 μ „λž΅(GenerationType)μ—λŠ” AUTO, IDENTITY, SEQUENCE, TABLE이 있으며, 각 μ „λž΅μ€ κΈ°λ³Έ ν‚€(Primary Key)λ₯Ό μƒμ„±ν•˜λŠ” 방식에 따라 λ‹€λ₯΄κ²Œ λ™μž‘ν•©λ‹ˆλ‹€.
  • AUTOλŠ” JPA(Java Persistence API)κ°€ μžλ™μœΌλ‘œ μ „λž΅μ„ μ„ νƒν•˜κ³ , IDENTITYλŠ” λ°μ΄ν„°λ² μ΄μŠ€μ˜ μžλ™ 증가 κΈ°λŠ₯을 μ‚¬μš©ν•˜λ©°, SEQUENCEλŠ” μ‹œν€€μŠ€ 객체λ₯Ό 톡해 κΈ°λ³Έ ν‚€(Primary Key)λ₯Ό μƒμ„±ν•˜κ³ , TABLE은 λ³„λ„μ˜ ν…Œμ΄λΈ”μ„ 톡해 고유 값을 κ΄€λ¦¬ν•©λ‹ˆλ‹€.
  • 이λ₯Ό 톡해 JPAλŠ” κΈ°λ³Έ ν‚€ 값을 μ‰½κ²Œ μƒμ„±ν•˜κ³  관리할 수 μžˆμ–΄, κ°œλ°œμžλŠ” κΈ°λ³Έ ν‚€ 생성에 λŒ€ν•΄ μ‹ κ²½ 쓰지 μ•Šμ•„λ„ λ©λ‹ˆλ‹€.