CodeGym /Java Course /All lectures for KO purposes /์ปฌ๋ ‰์…˜ ๋งคํ•‘

์ปฌ๋ ‰์…˜ ๋งคํ•‘

All lectures for KO purposes
๋ ˆ๋ฒจ 1 , ๋ ˆ์Šจ 847
์‚ฌ์šฉ ๊ฐ€๋Šฅ

์ปฌ๋ ‰์…˜ ๋ชฉ๋ก

์ง€๊ธˆ๊นŒ์ง€ ๊ฐ„๋‹จํ•œ ์œ ํ˜•์„ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ข€ ๋” ํฅ๋ฏธ๋กœ์šด ์งˆ๋ฌธ์ธ ๊ฐœ์ฒด ์ปฌ๋ ‰์…˜์„ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋„˜์–ด๊ฐˆ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฐ์ฒด๋ฅผ 5๊ฐœ ๊ทธ๋ฃน์œผ๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋ฐฐ์—ด - ๊ฐ์ฒด์˜ ๋ฐฐ์—ด
  • ๋ชฉ๋ก - ๊ฐœ์ฒด ๋ชฉ๋ก
  • ์„ธํŠธ - ๊ฐ์ฒด ์„ธํŠธ
  • ์ง€๋„ - ๊ฐ์ฒด ์‚ฌ์ „
  • ์ปฌ๋ ‰์…˜ - ๊ฐ์ฒด์˜ ์ปฌ๋ ‰์…˜

์ปฌ๋ ‰์…˜ ํ•„๋“œ๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค์˜ ์˜ˆ:

@Entity
@Table(name="user")
class User {
   @Id
   @Column(name="id")
   public Integer id;

   @magical-annotation
   public List messages;
}

๊ทธ๋ ‡๋‹ค๋ฉด ํ•˜๋‚˜์˜ ํ•„๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ๋งŽ์€ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์ด ๋งˆ๋ฒ• ๊ฐ™์€ ์ฃผ์„์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์ด ์ฃผ์„์„ @ElementCollection ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค . ์˜ˆ:

@Entity
@Table(name="user")
class User  {
   @Id
   @Column(name="id")
   public Integer id;

   @ElementCollection
   public List<String> messages;
}

๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑ๋˜์—ˆ์ง€๋งŒ ์‚ฌ์†Œํ•˜์ง€ ์•Š๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

๋ณด์กฐ ํ…Œ์ด๋ธ”

๋งŽ์€ ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  @ElementCollection ์ฃผ์„ ์œผ๋กœ ํ‘œ์‹œ๋œ Entity ํด๋ž˜์Šค์˜ ๋ชจ๋“  ํ•„๋“œ๋Š” ํŠน์ˆ˜ ๋ณด์กฐ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋…ผ๋ฆฌ์ ์ž…๋‹ˆ๋‹ค.

์ด ํ…Œ์ด๋ธ”์—๋Š” ๋‘ ๊ฐ€์ง€ ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Ordered (List, Map)์—๋Š” ์„ธ ๊ฐœ์˜ ์—ด์ด ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ‚ค ์—ด (์™ธ๋ž˜ ํ‚ค) โ€“ ์ƒ์œ„ ๊ฐœ์ฒด์˜ ID์— ๋Œ€ํ•œ ์ฐธ์กฐ์ž…๋‹ˆ๋‹ค.
    • ์ธ๋ฑ์Šค ์—ด - ์ปฌ๋ ‰์…˜์˜ ์œ„์น˜/์ธ๋ฑ์Šค์ž…๋‹ˆ๋‹ค.
    • ์š”์†Œ ์—ด - ๊ฐ’.
  • ์ •๋ ฌ๋˜์ง€ ์•Š์Œ (์„ธํŠธ)์—๋Š” ๋‘ ๊ฐœ์˜ ์—ด์ด ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ‚ค ์—ด (์™ธ๋ž˜ ํ‚ค) โ€“ ์ƒ์œ„ ๊ฐœ์ฒด์˜ ID์— ๋Œ€ํ•œ ์ฐธ์กฐ์ž…๋‹ˆ๋‹ค.
    • ์š”์†Œ ์—ด - ๊ฐ’.

์ฃผ์„์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์„ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

@CollectionTable(name="table_name")

์˜ˆ:

@Entity
@Table(name="user")
class User {
   @Id
   @Column(name="id")
   public Integer id;

   @ElementCollection
   @CollectionTable(name="user_message")
   public List<String> messages;
}

์ค‘์š”ํ•œ! @CollectionTable ์ฃผ์„์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ Hibernate๋Š” ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ํ•„๋“œ ์ด๋ฆ„(User ํด๋ž˜์Šค ๋ฐ ํ•„๋“œ ์ด๋ฆ„)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ…Œ์ด๋ธ” ์ด๋ฆ„ ์ž์ฒด๋ฅผ ๋นŒ๋“œํ•ฉ๋‹ˆ๋‹ค.๋ฉ”์‹œ์ง€ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ "User_messages"๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

์ปฌ๋ ‰์…˜ ์ปฌ๋ ‰์…˜

๊ทธ๋Ÿฌ๋‚˜ ๋ณด์กฐ ํ…Œ์ด๋ธ”์˜ ์ƒ์„ฑ์„ Hibernate์— ๋งก๊ธฐ์ง€ ๋ง๊ณ  ์ง์ ‘ ์ƒ์„ฑํ•˜๋„๋ก ํ•ฉ์‹œ๋‹ค. ๋จผ์ € ๋‘ ๊ฐœ์˜ ์—ด์ด ์žˆ๋Š” ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

CREATE TABLE user_message {
    user_id INT,
    message VARCHAR(255)
};

์ด ํ…Œ์ด๋ธ”์—๋Š” ๊ณ ์œ ํ•œ id-column์ด ์—†์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋ณด์กฐ ํ…Œ์ด๋ธ”์˜ ์ฃผ์š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ๋‹ค๋ฅธ ์œ ํ˜•์˜ ๋ณด์กฐ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด ์•Œ๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด์ œ ์ด ํ…Œ์ด๋ธ”์„ ํ•„๋“œ์— ๋งคํ•‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.๋ฉ”์‹œ์ง€์‚ฌ์šฉ์ž ํด๋ž˜์Šค ์—์„œ . ์ด๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค:

@Entity
@Table(name="user")
class User {
   @Id
   @Column(name="id")
   public Integer id;

   @ElementCollection
   @CollectionTable(name="user_message", joinColumns = @JoinColumn(name = "user_id"))
   @Column(name = "message")
   public Set<String> messages;
}

์—ฌ๊ธฐ์„œ ๋‘ ๊ฐ€์ง€์— ์ฃผ๋ชฉํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ์งธ, @Column(name = "message") ์ฃผ์„ ์œผ๋กœ ์ง€์ •๋œ ๋ฉ”์‹œ์ง€ ์—ด์€ user ํ…Œ์ด๋ธ”์ด ์•„๋‹ˆ๋ผ user_message ๋ณด์กฐ ํ…Œ์ด๋ธ”์— ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘˜์งธ, @JoinColumn(name = "user_id") ์ฃผ์„์—์„œ ์‚ฌ์šฉ์ž ํ…Œ์ด๋ธ”์˜ id๋ฅผ ์ฐธ์กฐํ•˜๋Š” user_id ์—ด์˜ ์ด๋ฆ„์„ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ Hibernate๊ฐ€ ๊ทธ๊ฒƒ๋“ค์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.

์ˆ˜์ง‘

๋ชฉ๋ก ๋˜๋Š” ๋ฐฐ์—ด์˜ ์ •๋ ฌ๋œ ์š”์†Œ๋ฅผ ๋ณด์กฐ ํ…Œ์ด๋ธ”์— ์ €์žฅํ•˜๋ ค๋ฉด ์„ธ ๊ฐœ์˜ ์—ด์ด ์žˆ๋Š” ํ…Œ์ด๋ธ”์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

CREATE TABLE user_message {
    user_id INT,
    index INT,
    message VARCHAR(255)
};

์—ด ์ด๋ฆ„ "์ธ๋ฑ์Šค"๊ฐ€ ๋งˆ์Œ์— ๋“ค์ง€ ์•Š๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ๋งคํ•‘ ์ค‘์— ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด @Index ์ฃผ์„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค .

์˜ˆ:

@Entity
@Table(name="user")
class User {
   @Id
   @Column(name="id")
   public Integer id;

   @ElementCollection
   @CollectionTable(name="user_message",
       	indexes = { @Index(columnList = "list_index") }
       	joinColumns = @JoinColumn(name = "user_id"))
   @Column(name = "message")
   public List<String> messages;
}

์ง€๋„ ์ˆ˜์ง‘

๋งˆ์ง€๋ง‰์œผ๋กœ ์ปฌ๋ ‰์…˜๋ฟ๋งŒ ์•„๋‹ˆ๋ผ HashMap๋„ ์ €์žฅํ•˜๊ณ  ์‹ถ๊ณ  ์ด๋ฅผ ์œ„ํ•ด ๋ณด์กฐ ํ…Œ์ด๋ธ”์— ๋‘ ๊ฐœ์˜ ์—ด์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

CREATE TABLE user_message {
    user_id INT,
    key VARCHAR(255),
    message VARCHAR(255)
};

Map์˜ ํ‚ค๋ฅผ ์ง€์ •ํ•˜๋ ค๋ฉด @MapKeyColumn ์ฃผ์„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค .

์˜ˆ:

@Entity
@Table(name="user")
class User {
   @Id
   @Column(name="id")
   public Integer id;

   @ElementCollection
   @CollectionTable(name="user_message", joinColumns = @JoinColumn(name = "user_id"))
   @MapKeyColumn(name = "key")
   @Column(name = "message")
   public Map<String, String> messages;
}

์ž์„ธํ•œ ๋‚ด์šฉ์€ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION