Home > Spring > πŸƒ[Spring] μœ μΌν•œ μ‹λ³„μž(Primary Key)

πŸƒ[Spring] μœ μΌν•œ μ‹λ³„μž(Primary Key)
Spring Framework

πŸƒ[Spring] μœ μΌν•œ μ‹λ³„μž(Primary Key).

Java λ°±μ—”λ“œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ user의 id μ •λ³΄λŠ” 보톡 μœ μΌν•œ 식별저(Primary Key) 둜 μ‚¬μš©λ˜λ©°, 각 μœ μ €λ³„λ‘œ κ²ΉμΉ˜μ§€ μ•ŠλŠ” κ³ μœ ν•œ κ°’μž…λ‹ˆλ‹€.

μ΄λŠ” λ°μ΄ν„°λ² μ΄μŠ€ μ„€κ³„μ—μ„œ 맀우 μ€‘μš”ν•œ κ°œλ…μœΌλ‘œ, μ‚¬μš©μžμ™€ 같은 μ—”ν‹°ν‹°(Entity)λ₯Ό κ³ μœ ν•˜κ²Œ μ‹λ³„ν•˜κΈ° μœ„ν•΄ Primary Keyλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

1️⃣ Primary Keyλž€?

  • Primary KeyλŠ” λ°μ΄ν„°λ² μ΄μŠ€ ν…Œμ΄λΈ”μ—μ„œ 각 행을 κ³ μœ ν•˜κ²Œ μ‹λ³„ν•˜λŠ” μ—΄(Column)μž…λ‹ˆλ‹€.
  • Primary KeyλŠ” ν…Œμ΄λΈ” λ‚΄μ—μ„œ 쀑볡될 수 μ—†μœΌλ©°, null 값을 κ°€μ§ˆ 수 μ—†μŠ΅λ‹ˆλ‹€.
  • 즉, 각 λ ˆμ½”λ“œλŠ” Primary Keyλ₯Ό 톡해 μ‹λ³„λ˜λ―€λ‘œ, user ν…Œμ΄λΈ”μ—μ„œ 각 μ‚¬μš©μžλŠ” μœ μΌν•œ idλ₯Ό κ°€μ Έμ•Ό ν•˜λ©°, 이λ₯Ό 톡해 μ‚¬μš©μžλ₯Ό ꡬ뢄할 수 μžˆμŠ΅λ‹ˆλ‹€.

2️⃣ 일반적인 예: User μ—”ν‹°ν‹°

user μ—”ν‹°ν‹°μ—μ„œ id ν•„λ“œλŠ” 주둜 Primary Key둜 μ„€μ •λ˜λ©°, λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ μžλ™μœΌλ‘œ μƒμ„±λ˜κ±°λ‚˜ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ 생성할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

User μ—”ν‹°ν‹° μ˜ˆμ‹œ (JPAλ₯Ό μ‚¬μš©ν•œ 경우)

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

@Entity
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id; // Primary Key, μœ μΌν•œ μ‹λ³„μž.
    
    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;
    }
}
  • @Id
    • id ν•„λ“œλŠ” Primary Keyμž„μ„ λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
  • @GeneratedValue
    • id 값이 μžλ™μœΌλ‘œ 생성됨을 μ˜λ―Έν•©λ‹ˆλ‹€.
    • GenerationType.IDENTITYλŠ” λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ μžλ™μœΌλ‘œ Primary Key 값을 μƒμ„±ν•˜λ„λ‘ μ§€μ •ν•˜λŠ” λ°©μ‹μž…λ‹ˆλ‹€.

3️⃣ Primary Key의 μ—­ν• .

  • 1. κ³ μœ μ„± 보μž₯
    • Primary KeyλŠ” 각 λ ˆμ½”λ“œλ₯Ό κ³ μœ ν•˜κ²Œ μ‹λ³„ν•˜λ―€λ‘œ, μ‚¬μš©μž μ •λ³΄μ—μ„œ idλŠ” 각 μœ μ €λ³„λ‘œ μœ μΌν•˜κ²Œ μ‘΄μž¬ν•˜λ©° μ ˆλŒ€ μ€‘λ³΅λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
  • 2. λΉ λ₯Έ 쑰회
    • Primary KeyλŠ” μΈλ±μŠ€κ°€ μžλ™μœΌλ‘œ μƒμ„±λ˜κΈ° λ•Œλ¬Έμ— λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ ν•΄λ‹Ή λ ˆμ½”λ“œλ₯Ό λΉ λ₯΄κ²Œ μ‘°νšŒν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 3. 관계 μ„€μ •
    • Primary KeyλŠ” λ‹€λ₯Έ ν…Œμ΄λΈ”μ—μ„œ Foreign Key둜 μ‚¬μš©λ˜μ–΄ ν…Œμ΄λΈ” κ°„μ˜ 관계λ₯Ό μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    • 예λ₯Ό λ“€μ–΄, Order ν…Œμ΄λΈ”μ—μ„œ user_idλŠ” User ν…Œμ΄λΈ”μ˜ Primary Keyλ₯Ό μ°Έμ‘°ν•˜μ—¬ μ£Όλ¬Έκ³Ό μ‚¬μš©μžλ₯Ό μ—°κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

4️⃣ ID 생성 방식.

IDλŠ” μ—¬λŸ¬ λ°©μ‹μœΌλ‘œ 생성될 수 있으며, κ°€μž₯ 많이 μ‚¬μš©ν•˜λŠ” 두 가지 방식은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

  • 1. μžλ™ 증가(Auto Increment)
    • λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ AUTO_INCREMENT 속성을 μ„€μ •ν•˜μ—¬ IDκ°€ μžλ™μœΌλ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.
    • 주둜 MySQL, PostgreSQL 같은 λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ μ‚¬μš©λ©λ‹ˆλ‹€.
  • 2. UUID(Universally Unique Identifier)
    • UUIDλŠ” μ „ μ„Έκ³„μ μœΌλ‘œ κ³ μœ ν•œ μ‹λ³„μžλ₯Ό μƒμ„±ν•˜λŠ” λ°©μ‹μœΌλ‘œ, 쀑볡될 κ°€λŠ₯성이 거의 μ—†μŠ΅λ‹ˆλ‹€.
    • JPAμ—μ„œλŠ” UUIDλ₯Ό Primary Key둜 μ‚¬μš©ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.
      • μ˜ˆμ‹œ
          @Id
          @GeneratedValue(generator = "UUID")
          @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
          private String id;
        

5️⃣ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ User ν…Œμ΄λΈ” μ˜ˆμ‹œ

CREATE TABLE users (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255),
    email VARCHAR(255)
);
  • μ—¬κΈ°μ„œ idλŠ” Primary Key둜, λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ μžλ™μœΌλ‘œ μ¦κ°€ν•˜λ©° 각 μ‚¬μš©μžλŠ” κ³ μœ ν•œ id 값을 κ°€μ§‘λ‹ˆλ‹€.

6️⃣ μ˜ˆμ‹œ: Primary Keyλ₯Ό ν†΅ν•œ μ‚¬μš©μž 쑰회

@RestController
@RequestMapping("/users")
public class UserController {
    
    @Autowired
    private UserRepository userRepository;
    
    @GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable Long id) {
        User user = userRepository.findById(id)
            .orElseThrow(() -> new UserNotFoundException("User not found with id " + id));
        return ResponseEntity.ok(user);
    }
}
  • μœ„ μ½”λ“œλŠ” idκ°€ Primary Key인 User μ—”ν‹°ν‹°λ₯Ό λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ μ‘°νšŒν•˜λŠ” μ˜ˆμ‹œμž…λ‹ˆλ‹€.
  • idλŠ” κ³ μœ ν•˜κΈ° λ•Œλ¬Έμ— 이 값을 톡해 νŠΉμ • μ‚¬μš©μžλ₯Ό μ •ν™•ν•˜κ²Œ μ‘°νšŒν•  수 μžˆμŠ΅λ‹ˆλ‹€.

7️⃣ μš”μ•½.

  • Java λ°±μ—”λ“œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ user의 idλŠ” 보톡 Primary Key둜 μ‚¬μš©λ˜λ©°, 각 μ‚¬μš©μžλŠ” κ³ μœ ν•œ id 값을 κ°€μ§‘λ‹ˆλ‹€.
  • Primary KeyλŠ” λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ 각 λ ˆμ½”λ“œλ₯Ό κ³ μœ ν•˜κ²Œ μ‹λ³„ν•˜λŠ” ν•„λ“œλ‘œ, μ ˆλŒ€ μ€‘λ³΅λ˜μ§€ μ•ŠμœΌλ©° null 값을 κ°€μ§ˆ 수 μ—†μŠ΅λ‹ˆλ‹€.
  • Primary Keyλ₯Ό 톡해 μ‚¬μš©μžλ₯Ό λΉ λ₯΄κ³  μ •ν™•ν•˜κ²Œ μ‘°νšŒν•  수 있으며, ν…Œμ΄λΈ” κ°„μ˜ 관계λ₯Ό μ„€μ •ν•˜λŠ” 데 μ€‘μš”ν•œ 역할을 ν•©λ‹ˆλ‹€.
  • idλŠ” μžλ™ 증가 λ°©μ‹μ΄λ‚˜ UUID λ°©μ‹μœΌλ‘œ 생성될 수 있으며, μ‚¬μš©μžμ˜ κ³ μœ μ„±μ„ 보μž₯ν•©λ‹ˆλ‹€.