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 ์ปฌ๋Ÿผ ์ƒ์„ฑ ๋ฐฉ์ง€ ๋ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์„ ๊น”๋”ํ•˜๊ฒŒ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.