Home > Backend Development > πŸ“š[Backend Development] mappedByλž€ λ¬΄μ—‡μΌκΉŒμš”?

πŸ“š[Backend Development] mappedByλž€ λ¬΄μ—‡μΌκΉŒμš”?
Backend Ddevelopment

β€œπŸ“š[Backend Development] mappedByλž€ λ¬΄μ—‡μΌκΉŒμš”?”

🍎 Intro.

  • mappedByλŠ” μ–‘λ°©ν–₯ μ—°κ΄€κ΄€κ³„μ—μ„œ μ‚¬μš©λ˜λŠ” μ†μ„±μœΌλ‘œ, μ—°κ΄€ κ΄€κ³„μ˜ 주인이 μ•„λ‹Œ(읽기 μ „μš©) μͺ½μ—μ„œ μ‚¬μš©ν•©λ‹ˆλ‹€.
  • 즉, μ™Έλž˜ ν‚€(FK)λ₯Ό κ΄€λ¦¬ν•˜μ§€ μ•ŠλŠ” μͺ½μ—μ„œ mappedByλ₯Ό μ‚¬μš©ν•˜μ—¬ μ—°κ΄€ 관계λ₯Ό λ§€ν•‘ν•©λ‹ˆλ‹€.

βœ…1️⃣ mappedBy의 ν•„μš”μ„±.

  • μ–‘λ°©ν–₯ κ΄€κ³„μ—μ„œλŠ” 두 개의 μ—”ν‹°ν‹°κ°€ μ„œλ‘œλ₯Ό μ°Έμ‘°ν•˜κ²Œ λ˜λŠ”λ°, JPAλŠ” μ™Έλž˜ ν‚€(FK)λ₯Ό 관리할 β€œμ£ΌμΈβ€μ„ ν•˜λ‚˜λ§Œ μ§€μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€.
    • μ΄λ•Œ, μ—°κ΄€ κ΄€κ³„μ˜ 주인이 μ•„λ‹Œ μͺ½μ—μ„œ mappedByλ₯Ό μ‚¬μš©ν•˜μ—¬ 주인을 λͺ…μ‹œν•©λ‹ˆλ‹€.

βœ…2️⃣ @OneToOne μ–‘λ°©ν–₯ κ΄€κ³„μ—μ„œ mappedBy μ‚¬μš© 예제

1️⃣ User μ—”ν‹°ν‹° (μ—°κ΄€ κ΄€κ³„μ˜ 주인)

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = Generation.IDENTITY)
    private Long id;
    
    private String username;
    
    @OneToOne
    @JoinColumn(name = "profile_id") // FKλ₯Ό κ΄€λ¦¬ν•˜λŠ” 주인 (user ν…Œμ΄λΈ”μ— profile_id FK 생성)
    private UserProfile profile;
    
    // Getter, Setter
}

2️⃣ UserProfile μ—”ν‹°ν‹°(mappedBy μ‚¬μš©)

@Entity
public class UserProfile {
    @Id
    @GenerationValue(strategy = Generation.IDENTITY)
    private Long id;
    
    private String bio;
    private String website;
    
    @OneToOne(mappedBy = "profile") // User μ—”ν‹°ν‹°μ˜ profile ν•„λ“œκ°€ κ΄€κ³„μ˜ 주인
    private User user;
    
    // Getter, Setter
}

βœ…3️⃣ mappedBy = β€œprofileβ€μ˜ 의미

  • β€œprofile”은 User μ—”ν‹°ν‹°μ˜ profile ν•„λ“œλͺ…을 κ°€λ¦¬ν‚΅λ‹ˆλ‹€.
  • 즉, 이 κ΄€κ³„μ˜ 주인은 User.profile이며, UserProfile μ—”ν‹°ν‹°λŠ” 읽기 μ „μš©μž…λ‹ˆλ‹€.
  • λ”°λΌμ„œ UserProfile.user ν•„λ“œλŠ” μ™Έλž˜ ν‚€(FK)λ₯Ό μƒμ„±ν•˜μ§€ μ•Šκ³ , λ§€ν•‘λ§Œ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

βœ…4️⃣ λ°μ΄ν„°λ² μ΄μŠ€ ν…Œμ΄λΈ” ꡬ쑰

  • μœ„ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ user ν…Œμ΄λΈ”λ§Œ profile_idλΌλŠ” FK μ»¬λŸΌμ„ κ°€μ§€λ©°, user_profile ν…Œμ΄λΈ”μ—λŠ” μΆ”κ°€ 컬럼이 μƒμ„±λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

πŸ“Š 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 ν…Œμ΄λΈ”μ—λŠ” FK 컬럼이 μ—†μŠ΅λ‹ˆλ‹€.

βœ…5️⃣ mappedByλ₯Ό μ‚¬μš©ν•œ 데이터 쑰회

βœ… User ➞ UserProfile 쑰회(κ°€λŠ₯ βœ…)

User user = entityManager.find(User.class, 1L);
UserProfile profile = user.getProfile(); // 정상 μž‘λ™

βœ… UserProfile ➞ User 쑰회(κ°€λŠ₯ βœ…)

UserProfile profile = entityManager.find(UserProfile.class, 101L);
User user = profile.getUser(); // mappedByλ₯Ό μ‚¬μš©ν–ˆμœΌλ―€λ‘œ κ°€λŠ₯!

πŸš€ 정리.

  • βœ”οΈ μ—°κ΄€ κ΄€κ³„μ˜ 주인(Owner)이 μ•„λ‹Œ μͺ½μ—μ„œ mappedByλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.
  • βœ”οΈ β€œmappedBy = 주인 μ—”ν‹°ν‹° ν•„λ“œλͺ…β€μœΌλ‘œ μ„€μ •ν•΄μ•Ό ν•œλ‹€.
  • βœ”οΈ μ™Έλž˜ ν‚€(FK)λŠ” mappedByλ₯Ό μ‚¬μš©ν•œ μͺ½μ΄ μ•„λ‹ˆλΌ 주인이 κ΄€λ¦¬ν•œλ‹€.
  • βœ”οΈ mappedByλŠ” 읽기 μ „μš©μ΄λ―€λ‘œ @JoinColumn을 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€.

πŸ“Œ mappedByλ₯Ό μ‚¬μš©ν•˜λ©΄ λΆˆν•„μš”ν•œ FK 컬럼 생성 λ°©μ§€ 및 λ°μ΄ν„°λ² μ΄μŠ€ ν…Œμ΄λΈ”μ„ κΉ”λ”ν•˜κ²Œ μœ μ§€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.