Home > Backend Development > πŸ“š[Backend Development] 단방ν–₯κ³Ό @OneToOneμ΄λž€ λ¬΄μ—‡μΌκΉŒμš”?

πŸ“š[Backend Development] 단방ν–₯κ³Ό @OneToOneμ΄λž€ λ¬΄μ—‡μΌκΉŒμš”?
Backend Ddevelopment

β€œπŸ“š[Backend Development] 단방ν–₯κ³Ό @OneToOneμ΄λž€ λ¬΄μ—‡μΌκΉŒμš”?”

🍎 Intro.

  • 단방ν–₯ @OneToOne κ΄€κ³„λŠ” μ—”ν‹°ν‹° κ°„μ˜ 1:1 관계λ₯Ό λ§€ν•‘ν•  λ•Œ, ν•œμͺ½ μ—”ν‹°ν‹°μ—μ„œλ§Œ 관계λ₯Ό κ΄€λ¦¬ν•˜λŠ” λ°©μ‹μž…λ‹ˆλ‹€.
    • 즉, ν•œ μ—”ν‹°ν‹°μ—μ„œλ§Œ λ‹€λ₯Έ μ—”ν‹°ν‹°λ₯Ό μ°Έμ‘°ν•˜κ³ , λ°˜λŒ€μͺ½μ—μ„œλŠ” 이λ₯Ό μ•Œμ§€ λͺ»ν•˜λŠ” μƒνƒœμž…λ‹ˆλ‹€.

βœ…1️⃣ 예제 μ½”λ“œ

  • 예λ₯Ό λ“€μ–΄, User 엔티티와 UserProfile μ—”ν‹°ν‹°κ°€ 1:1 관계λ₯Ό κ°€μ§„λ‹€κ³  κ°€μ •ν•΄λ΄…μ‹œλ‹€.

πŸ“ User μ—”ν‹°ν‹°μ—μ„œ UserProfile μ—”ν‹°ν‹°λ₯Ό 단방ν–₯으둜 μ°Έμ‘°ν•˜λŠ” 경우:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String username;
    
    @OneToOne
    @JoinColumn(name = "profile_id") // User ν…Œμ΄λΈ”μ˜ profile_id 컬럼이 UserProfile의 idλ₯Ό μ°Έμ‘°
    private UserProfile profile;
    
    // Getter, Setter
}

πŸ“ UserProfile μ—”ν‹°ν‹°:

@Entity
public class UserProfile {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String bio;
    private String website;
    
    // Getter, Setter
}

βœ… μ„€λͺ…:

    1. @OneToOne을 μ‚¬μš©ν•˜μ—¬ User μ—”ν‹°ν‹°κ°€ UserProfile μ—”ν‹°ν‹°λ₯Ό μ°Έμ‘°ν•©λ‹ˆλ‹€.
    1. @JoinColumn(name = β€œprofile_id”)λ₯Ό μ‚¬μš©ν•˜μ—¬ User ν…Œμ΄λΈ”μ— profile_id 컬럼이 μƒμ„±λ©λ‹ˆλ‹€.
      • User ν…Œμ΄λΈ”μ— profile_idλΌλŠ” μ™Έλž˜ ν‚€(FK) μ»¬λŸΌμ„ μΆ”κ°€ν•˜κ³ , 이 컬럼이 UserProfile ν…Œμ΄λΈ”μ˜ id(PK)λ₯Ό μ°Έμ‘°ν•˜λ„λ‘ λ§Œλ“­λ‹ˆλ‹€.
      • 즉, User ν…Œμ΄λΈ”μ˜ profile_idκ°€ UserProfile ν…Œμ΄λΈ”μ˜ idλ₯Ό μ°Έμ‘°ν•˜λŠ” FK이닀.
    1. ν•˜μ§€λ§Œ UserProfile μ—”ν‹°ν‹°μ—λŠ” Userμ™€μ˜ 관계λ₯Ό μ•Œ 수 μžˆλŠ” 정보가 μ—†μŠ΅λ‹ˆλ‹€. ➞ 이것이 단방ν–₯ κ΄€κ³„μž…λ‹ˆλ‹€.

βœ… μ‚΄μ œ λ°μ΄ν„°λ² μ΄μŠ€ ν…Œμ΄λΈ” μ˜ˆμ‹œ:

  • 이 μ½”λ“œλ₯Ό 기반으둜 JPAκ°€ μƒμ„±ν•˜λŠ” ν…Œμ΄λΈ”μ„ 보면 λ‹€μŒκ³Ό 같이 λ©λ‹ˆλ‹€.

πŸ“Š user ν…Œμ΄λΈ”

id username profile_id(FK)
1 Alice 101
2 Bob 102

πŸ“Š user_profile ν…Œμ΄λΈ”

id bio website
101 β€œGamer” β€œalice.com”
102 β€œDeveloper” β€œbob.dev”
  • πŸ“Œ 즉, user.profile_idλŠ” user_profile.idλ₯Ό μ°Έμ‘°(FK)ν•˜λŠ” κ΅¬μ‘°μž…λ‹ˆλ‹€.
    • λ”°λΌμ„œ UserProfile μ—”ν‹°ν‹°μ—λŠ” profile_idκ°€ λ”°λ‘œ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
    • λŒ€μ‹  κΈ°λ³Έ ν‚€(id)κ°€ User μ—”ν‹°ν‹°μ˜ μ™Έλž˜ ν‚€(profile_id)둜 μ‚¬μš©λ©λ‹ˆλ‹€.

βœ…2️⃣ 단방ν–₯ κ΄€κ³„μ˜ νŠΉμ§•.

βœ… μž₯점.

  • ꡬ쑰가 λ‹¨μˆœν•˜κ³  μ΄ν•΄ν•˜κΈ° 쉽닀.
  • ν•œμͺ½μ—μ„œλ§Œ μ°Έμ‘°ν•˜λ―€λ‘œ λΆˆν•„μš”ν•œ 연관관계 λ‘œλ”©μ„ λ°©μ§€ν•  수 μžˆλ‹€.

❌ 단점.

  • λ°˜λŒ€μͺ½(UserProfile)μ—μ„œ Userλ₯Ό μ‘°νšŒν•  방법이 μ—†λ‹€.
  • UserProfile이 μžμ‹ μ„ μ°Έμ‘°ν•˜λŠ” Userκ°€ λˆ„κ΅¬μΈμ§€ μ•Œκ³  μ‹Άλ‹€λ©΄ λ³„λ„μ˜ 쿼리λ₯Ό μž‘μ„±ν•΄μ•Ό ν•œλ‹€.

βœ…3️⃣ 단방ν–₯ 관계 쑰회.

  • μ‚¬μš©μžκ°€ ν”„λ‘œν•„ 정보λ₯Ό κ°€μ Έμ˜€λŠ” μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
User user = entityManager.find(User.class, 1L);
UserProfile profile = user.getProfile(); // User -> UserProfile 쑰회 κ°€λŠ₯.