Posts Spring JPA 2
Post
Cancel

Spring JPA 2

Spring JPA 2


๐Ÿ’ฟ Entity Mapping

@Entity

  • Entity๋Š” ๊ฐ์ฒด ๊ด€์ ์—์„œ ๋ถ€๋ฅด๋Š” ์ด๋ฆ„
  • ๋ณดํ†ต Class์™€ ๊ฐ™์€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์Œ
  • Entity์˜ ์ด๋ฆ„์€ JQL์—์„œ ์‚ฌ์šฉ

@Table

  • Relation(database) ๊ด€์ ์—์„œ ๋ถ€๋ฅด๋Š” ์ด๋ฆ„
  • @Entity์˜ ์ด๋ฆ„์ด ๊ธฐ๋ณธ๊ฐ’
  • Table์˜ ์ด๋ฆ„์€ SQL์—์„œ ์‚ฌ์šฉ

@Id

  • Entity์˜ Primary Key๋ฅผ ๋งคํ•‘ํ•  ๋–„ ์‚ฌ์šฉ
  • java์˜ ๋ชจ๋“  primitive ์™€ Wrapper ํƒ€์ž… ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • Date๋ž‘ BigDecimal, BigInteger๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅ
  • ๋ณตํ•ฉํ‚ค๋ฅผ ๋งŒ๋“ค์–ด ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ธด ์žˆ์Œ

@GenerativeValue

  • Primary Key์˜ ์ƒ์„ฑ ๋ฐฉ๋ฒ•์„ ๋งคํ•‘ํ•˜๋Š” Annotation
  • ์ƒ์„ฑ ์ „๋žต๊ณผ ์ƒ์„ฑ๊ธฐ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ
    • ๊ธฐ๋ณธ ์ „๋žต: AUTO(DB์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ์ „๋žต ์„ ํƒ)
    • TABLE, SEQUENCE, IDENTITY ์ค‘ ํ•˜๋‚˜

@Column

  • unique, nullable, length, ColumnDefinition ๋“ฑ Row์˜ ํŠน์„ฑ ์ •์˜

@Temporal

  • Date ์™€ Calendar

@Transient

  • Column์œผ๋กœ ๋งคํ•‘ํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ์‚ฌ์šฉ

Tip

  • spring.jpa.show-sql=true
  • spring.jpa.properties.hibernate.format_sql=true

๐Ÿ’ฟ @Value ํƒ€์ž… ๋งคํ•‘

Entity ์™€ Value

  • ์‹๋ณ„์ž๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๋Š”๊ฐ€
  • ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌํ•ด์•ผ ํ•˜๋Š”๊ฐ€

Value ํƒ€์ž… ์ข…๋ฅ˜

  • ๊ธฐ๋ณธ ํƒ€์ž…(String, Date, Boolean)
  • Composite Value ํƒ€์ž…
  • Collection Value ํƒ€์ž…
    • ๊ธฐ๋ณธ ํƒ€์ž…์˜ ์ฝœ๋ ‰์…˜
    • Composite ํƒ€์ž…์˜ ์ฝœ๋ ‰์…˜

Composite Value ํƒ€์ž…

  • @Embedable
  • @Embedded
  • @AttributeOverrides
  • AttributeOverride

๐Ÿ’ฟ ๊ด€๊ณ„ ๋งคํ•‘

  • 2๊ฐœ์˜ Entity์˜ ๊ด€๊ณ„๋ฅผ ์„ค์ •
    • ๋‘˜ ์ค‘ ํ•˜๋‚˜๋Š” ๊ด€๊ณ„์˜ ์ฃผ์ธ์ด ๋˜์–ด์•ผ ํ•จ
    • ๋‹ค๋ฅธ ์ชฝ์€ ์ข…์†๋œ ์ชฝ
    • ํ•ด๋‹น ๊ด€๊ณ„์˜ ๋ฐ˜๋Œ€์ชฝ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ฐ€์ง„ ์ชฝ์ด ์ฃผ์ธ
  • ๋‹จ๋ฐฉํ–ฅ
    • ๋‹จ๋ฐฉํ–ฅ์—์„œ๋Š” ๊ด€๊ณ„์˜ ์ฃผ์ธ์ด ๋ช…ํ™•
      • ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•œ ์ชฝ์ด ๊ด€๊ณ„์˜ ์ฃผ์ธ
    • ๋‹จ๋ฐฉํ–ฅ @ManyToOne
      • ๊ธฐ๋ณธ๊ฐ’์€ FK ์ƒ์„ฑ
    • ๋‹จ๋ฐฉํ–ฅ @OneToMany
      • ๊ธฐ๋ณธ๊ฐ’์€ ์กฐ์ธ ํ…Œ์ด๋ธ” ์ƒ์„ฑ
  • ์–‘๋ฐฉํ–ฅ
    • FK๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ชฝ์ด Owner ๋”ฐ๋ผ์„œ @ManytoOne ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ชฝ์ด ์ฃผ์ธ
    • ์ฃผ์ธ์ด ์•„๋‹Œ ์ชฝ์€ mappedby๋ฅผ ํ†ตํ•ด ํ•„๋“œ๋ฅผ ์„ค์ •
      • @ManyToOne(์ด์ชฝ์ด ์ฃผ์ธ)
      • @OneToMany(mappedby)
      • ์ฃผ์ธ์—๊ฒŒ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•ด์•ผ DB์— ๋ฐ˜์˜์ด ๋จ(์ค‘์š”)
        • mappedby์ชฝ์— ๋”ํ•ด์„œ saveํ•ด๋„ DB ์ ์šฉ ์•ˆ๋จ

๐Ÿ’ฟ Entity ์ƒํƒœ์™€ Cascade

Cascade: Entity์˜ ์ƒํƒœ๋ฅผ ์ „์ด ์‹œํ‚ค๋Š” ์˜ต์…˜

Entity์˜ ์ƒํƒœ

  • Transient: JPA๊ฐ€ ๋ชจ๋ฅด๋Š” ์ƒํƒœ
  • Persistent: JPA๊ฐ€ ๊ด€๋ฆฌ์ค‘์ธ ์ƒํƒœ(1์ฐจ ์บ์‰ฌ, Dirty-Checking, WriteBehind)
  • Detached: JPA๊ฐ€ ๋”์ด์ƒ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๋Š” ์ƒํƒœ
  • Removed: JPA๊ฐ€ ๊ด€๋ฆฌํ•˜๊ธด ํ•˜์ง€๋งŒ ์‚ญ์ œํ•˜๊ธฐ๋กœ ํ•œ ์ƒํƒœ

image


Entity Fetch Join

  • ์—ฐ๊ด€๋œ Entity๋ฅผ ํ•จ๊ป˜ ์กฐํšŒ
  • select m from Member m join fetch m.team
    • SQL: Select M.,T. FROM MEMBER M INNER JOIN TEAM T on M.TEAM_ID=T.ID
This post is licensed under CC BY 4.0 by the author.

Spring JPA

Spring