Spring Data JPA - @MapsId

๋ชฝ๋ฃจ๋ฌธยท2023๋…„ 12์›” 10์ผ
0

Spring Data JPA

๋ชฉ๋ก ๋ณด๊ธฐ
4/7
post-thumbnail

๐Ÿ“ Spring Data JPA์˜ MapsId

Database ์„ค๊ฒŒ๋ฅผ ์ง„ํ–‰ํ•˜๋‹ค๋ณด๋ฉด 1:1 ๊ด€๊ณ„์—์„œ ๋ถ€๋ชจ์™€ ์ž์‹์ฒ˜๋Ÿผ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์— ์ข…์†๋˜์–ด ์žˆ๋Š” ํ…Œ์ด๋ธ”์ด ์„ค๊ณ„๋  ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.์ด๋Ÿฐ ๊ฒฝ์šฐ ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์˜ PK๋ฅผ ์ž์‹ํ…Œ์ด๋ธ”์—์„œ PK์ด์ž FK๋กœ ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ JPA Entity ์„ค๊ณ„๋ฅผ ํ• ๋•Œ ๋‹จ์ˆœ @JoinColumn Annotation์œผ๋กœ ์„ค๊ณ„ํ•˜๊ฒŒ๋˜๋ฉด ๋ถ€๋ชจํ…Œ์ด๋ธ”์—์„œ PK๊ฐ€ ์—†๋‹ค๊ณ  JPA์—์„œ ์˜ค๋ฅ˜๋กœ ๋ฐ›์•„๋“ค์ผ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ PK ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋”๋ผ๋„ PK ์™€ FK๊ฐ€ ๋”ฐ๋กœ ์ƒ๊ฒจ๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
๋”ฐ๋ผ์„œ MapsId ๋ผ๋Š” Annotation์„ ์ด์šฉํ•ด์„œ FK ์ปฌ๋Ÿผ์„ ์ค„์ด๋Š” ๋™์‹œ์— PK ์™€ FK๋ฅผ ํ•ฉ์ณ์„œ ์„ค๊ณ„๊ฐ€ ๋˜๋„๋ก ๊ธฐ๋กํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.




๐Ÿšฉ 1:1 ๊ด€๊ณ„์—์„œ์˜ @MapsId


๐Ÿ“ ๋ถ€๋ชจ Entity

โ–  Source Code

@Entity(name = "speaker_info")
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class SpeakerInfo {
    @Id
    @Column(name = "speaker_id", length = 15)
    private String speakerId;

    @Column(name = "lte_number", length = 11, nullable = false)
    private String lteNumber;

    @ManyToOne(targetEntity = RegionInfo.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "region_code", nullable = false)
    private RegionInfo regionInfo;

    @Column(name = "latitude")
    private Double latitude;

    @Column(name = "longitude")
    private Double longitude;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;
}

โ–  ์„ค๋ช…

๋ถ€๋ชจ Entity์˜ ์„ค๊ณ„๋Š” ์œ„์™€ ๊ฐ™์ด ๋˜์–ด ์žˆ๋‹ค. ์Šคํ”ผ์ปค ์ •๋ณด์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ ์ž์‹ Entity๋กœ๋Š” ์Šคํ”ผ์ปค์˜ ์„ค์ • ์ •๋ณด์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋„๋ก ์„ค๊ณ„๊ฐ€ ๋˜์–ด ์žˆ๋‹ค.
์•„๋ž˜์˜ ์ž์‹ Entity์˜ ์ •๋ณด๋ฅผ ๋ณด๋„๋ก ํ•˜์ž.


๐Ÿ“ ์ž์‹ Entity

โ–  Source Code

@Entity(name = "speaker_control_value_info")
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class SpeakerControlValueInfo {
    @Id
    private String speakerInfoId;
    
    @MapsId(value = "speakerInfoId")
    @OneToOne(targetEntity = SpeakerInfo.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "speaker_info_id")
    private SpeakerInfo speakerInfo;

    //    SubCommand 1
    @Column(name = "bgm_volume_ch1")
    @ColumnDefault("0")
    private byte bgmVolumeCh1;
}

โ–  ์„ค๋ช…

์ž์‹ Entity์˜ ์„ค๊ณ„๋Š” ์œ„์™€ ๊ฐ™์ด ๋˜์–ด ์žˆ๋‹ค. ์Šคํ”ผ์ปค ์ •๋ณด๋ฅผ ๋ถ€๋ชจ Entity๋กœ์„œ 1:1๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋„๋ก ์„ค๊ณ„ํ•˜์˜€๋‹ค. ์Šคํ”ผ์ปค ์ •๋ณด๊ฐ€ ์žˆ์–ด์•ผ ์Šคํ”ผ์ปค ์„ค์ •์ •๋ณด๊ฐ€ ์žˆ๋Š” ๊ฒƒ ์ด๋ฏ€๋กœ 1:1๋กœ์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—‡๋‚˜๊ฐˆ ์ด์œ ๋Š” ์—†์œผ๋ฏ€๋กœ ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ฐฐ์ œํ•˜๋„๋ก ํ•œ๋‹ค. Annotation์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž.

  • @OneToOne
    ์Šคํ”ผ์ปค ์ •๋ณด ํ…Œ์ด๋ธ”์ธ ๋ถ€๋ชจ Entity์™€ 1:1๋กœ ๋งตํ•‘์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ํ•ด๋‹น Annotation์„ ํ‘œ์‹œํ•˜์˜€๋‹ค.
    ์ธ์ž๋กœ targetEntity์™€ fetch์˜ ์กฐ๊ฑด์„ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.
  • @JoinColumn
    ์ž์‹ Entity์˜ Column์ด ๋ถ€๋ชจ Entity์˜ ์–ด๋Š Column ์—ฐ๊ฒฐ๋ ์ง€ ์„ค์ •ํ•˜๋Š” Annotation์ด๋‹ค.
  • @Id
    ์ž์‹ Entity์˜ ์‹ค์ œ PK๋กœ์„œ Database์—์„œ ์‹ค์ œ PK๋กœ ์ž‘๋™๋  uniqueํ•œ key์ด๋‹ค.
  • @MapsId
    ์ž์‹ Entity์—์„œ Database ์ƒ์ด ์•„๋‹Œ JPA ์ƒ์—์„œ ์‹ค์ œ๋กœ ์‚ฌ์šฉ๋˜๋Š” PK๋ฅผ ์„ค์ •ํ•˜๋Š” Annotation์ด๋‹ค.





๐Ÿ“Œ ๋งˆ๋ฌด๋ฆฌ

@MapsId Annotation์— ๊ด€ํ•˜์—ฌ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค. ๋ธ”๋กœ๊ทธ๋ฅผ ๋’ค์ง€๋‹ค๋ณด๋ฉด ๋‹จ์ˆœํžˆ Column๋ฅผ ์ค„์—ฌ์ฃผ๋Š” ์—ญํ• ์ด๋ผ๊ณ  ๋‚˜์˜ค๋Š”๋ฐ, ์ •ํ™•ํ•œ ๊ฐœ๋…์„ ์•„์ง ๊นจ๋‹ซ์ง€ ๋ชปํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋งŒ ๊นจ๋‹ฌ์€๊ฑฐ ๊ฐ™์•„์„œ ์ข€ ์•„์‰ฝ๋‹ค. @MapsId ์˜ ๊ฐœ๋…์— ๋Œ€ํ•œ ๊ณต๋ถ€๋ฅผ ์ข€๋” ํ•ด์„œ ๋‚˜์ค‘์— ๊ธฐ๋กํ•ด ๋ณด๋ ค๊ณ  ํ•˜๊ณ , 1:N์—์„œ @MapsId๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ ๊ทธ๊ฒƒ๋„ ์ฐพ์•„์„œ ๊ธฐ๋กํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

profile
์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์ •๋ฆฌํ•˜๊ณ , ์ƒˆ๋กœ์šด ๊ฒƒ์„ ์•Œ๊ธฐ์œ„ํ•ด ๋„์ ์ด๋Š”๊ณณ..

0๊ฐœ์˜ ๋Œ“๊ธ€