🌻 JAVA/μžλ°” ORM ν‘œμ€€ JPA ν”„λ‘œκ·Έλž˜λ°

9-1) κ°’ νƒ€μž… - μž„λ² λ””λ“œ νƒ€μž…(Embedded Type)

iseunghan 2020. 11. 24. 09:23
λ°˜μ‘ν˜•

μžλ°” ORM ν‘œμ€€ JPA ν”„λ‘œκ·Έλž˜λ° - κΈ°λ³ΈνŽΈμ„ κ³΅λΆ€ν•˜λ©° μ •λ¦¬ν•œ λ‚΄μš©μž…λ‹ˆλ‹€.

 

μžλ°” ORM ν‘œμ€€ JPA ν”„λ‘œκ·Έλž˜λ° - 기본편 - μΈν”„λŸ°

JPAλ₯Ό 처음 μ ‘ν•˜κ±°λ‚˜, μ‹€λ¬΄μ—μ„œ JPAλ₯Ό μ‚¬μš©ν•˜μ§€λ§Œ κΈ°λ³Έ 이둠이 λΆ€μ‘±ν•˜μ‹  뢄듀이 JPA의 κΈ°λ³Έ 이둠을 νƒ„νƒ„ν•˜κ²Œ ν•™μŠ΅ν•΄μ„œ μ΄ˆλ³΄μžλ„ μ‹€λ¬΄μ—μ„œ μžμ‹ μžˆκ²Œ JPAλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄ˆκΈ‰ μ›Ή 개발 ν”„λ‘œκ·ΈοΏ½

www.inflearn.com

 

JPA의 데이터 νƒ€μž… λΆ„λ₯˜

  • μ—”ν‹°ν‹° νƒ€μž…
    • @Entity둜 μ •μ˜ν•˜λŠ” 객체
    • 데이터가 변해도 μ‹λ³„μžλ‘œ μ§€μ†ν•΄μ„œ 좔적 κ°€λŠ₯
    • 예) νšŒμ› μ—”ν‹°ν‹°μ˜ ν‚€λ‚˜ λ‚˜μ΄ 값을 변경해도 μ‹λ³„μžλ‘œ 인식 κ°€λŠ₯
  • κ°’ νƒ€μž…
    • int, Integer, String 처럼 λ‹¨μˆœνžˆ κ°’μœΌλ‘œ μ‚¬μš©ν•˜λŠ” μžλ°” κΈ°λ³Έ νƒ€μž…μ΄λ‚˜ 객체
    • μ‹λ³„μžκ°€ μ—†κ³  κ°’λ§Œ μžˆμœΌλ―€λ‘œ λ³€κ²½μ‹œ 좔적 λΆˆκ°€
    • 값을 λ³€κ²½ν•  λ•Œ, μ™„μ „νžˆ ν†΅μœΌλ‘œ μƒˆλ‘œμš΄ μΈμŠ€ν„΄μŠ€λ‘œ ꡐ체해야 함! (주의)

 

κ°’ νƒ€μž… λΆ„λ₯˜

  • κΈ°λ³Έκ°’ νƒ€μž…
    • μžλ°” κΈ°λ³Έ νƒ€μž…(int, double)
    • 래퍼 클래슀(Integer, Long)
    • String
  • μž„λ² λ””λ“œ νƒ€μž…(Embedded Type, 볡합 κ°’ νƒ€μž…)
  • μ»¬λ ‰μ…˜ κ°’ νƒ€μž…(Collection Value Type)

 

 

μž„λ² λ””λ“œ νƒ€μž…

  • μƒˆλ‘œμš΄ κ°’ νƒ€μž…μ„ 직접 μ •μ˜ν•  수 있음
  • JPAλŠ” μž„λ² λ””λ“œ νƒ€μž…(Embedded Type) 이라 함
  • 주둜 κΈ°λ³Έ κ°’ νƒ€μž…μ„ λͺ¨μ•„μ„œ λ§Œλ“€μ–΄μ„œ 볡합 κ°’ νƒ€μž…μ΄λΌκ³ λ„ 함
  • int, Stringκ³Ό 같은 κΈ°λ³Έ κ°’ νƒ€μž…

 

Member μ—”ν‹°ν‹°μ˜ startDate, endDate λ₯Ό 볡합 κ°’ νƒ€μž…μœΌλ‘œ λ¬Άμ–΄μ„œ PeriodλΌλŠ” μž„λ² λ””λ“œ νƒ€μž…μ„ 생성 ν•  것이고,

city, street, zipcode λ₯Ό 볡합 κ°’ νƒ€μž…μœΌλ‘œ λ¬Άμ–΄μ„œ AddressλΌλŠ” μž„λ² λ””λ“œ νƒ€μž…μ„ 생성 ν•  것이닀.

 

μž„λ² λ””λ“œ νƒ€μž… μ‚¬μš©λ²•

  • @Embeddedable : κ°’ νƒ€μž…μ„ μ •μ˜ν•˜λŠ” 곳에 ν‘œμ‹œ
  • @Embedded : κ°’ νƒ€μž…μ„ μ‚¬μš©ν•˜λŠ” 곳에 ν‘œμ‹œ (λ‘˜ 쀑 ν•˜λ‚˜λŠ” μƒλž΅ κ°€λŠ₯! ν•˜μ§€λ§Œ λ‘˜ λ‹€ μ‚¬μš©ν•˜λŠ” κ±Έ ꢌμž₯)
  • κΈ°λ³Έ μƒμ„±μž(Constructor) ν•„μˆ˜!

@Entity
public class Member {
	..
    
    @Embedded // κ°’ νƒ€μž…μ„ μ‚¬μš©ν•˜λŠ” 곳에 ν‘œμ‹œ!
    private Period workPeriod;
    
    @Embedded // κ°’ νƒ€μž…μ„ μ‚¬μš©ν•˜λŠ” 곳에 ν‘œμ‹œ!
    private Address homeAddress;

	..
}

 

@Embeddedable // κ°’ νƒ€μž…μ΄λΌκ³  μ •μ˜!
public class Period {
	
    private LocalDateTime startDate;
    private LocalDateTime endDate;
    
    public Period() {
    	// κΈ°λ³Έ μƒμ„±μž ν•„μˆ˜
    }
    
    ..
}

 

@Embeddedable // κ°’ νƒ€μž…μ„ μ •μ˜!
public class Address {

      private String city;
      private String street;
      private String zipcode;

      public Address() {
          // κΈ°λ³Έ μƒμ„±μž ν•„μˆ˜
      }
    ..
}

 

κ°’ νƒ€μž…μ— 값을 넣을 λ•Œ

μΈμŠ€ν„΄μŠ€ μƒμ„±ν•΄μ„œ λ„£μ–΄μ£Όλ©΄ 끝! (κ°„λ‹¨ν•˜λ‹€)

// JpaMain.java

{
	.. μƒλž΅
    
      Member member = new Member();
      ..
      member.setHomeAddress(new Address("city", "street", "zipcode"));
      member.setWorkPeriod(new Period(LocalDateTime.now(), LocalDateTime.now()));
      em.persist(member);

    ..
}
    

 

μž„λ² λ””λ“œ νƒ€μž…μ˜ μž₯점

  • μž¬μ‚¬μš©
  • 높은 응집도
  • Period.isWork() 처럼 ν•΄λ‹Ή κ°’ νƒ€μž…λ§Œ μ‚¬μš©ν•˜λŠ” 의미 μžˆλŠ” λ©”μ†Œλ“œλ₯Ό λ§Œλ“€ 수 μžˆλ‹€.
  • μž„λ² λ””λ“œ νƒ€μž…μ„ ν¬ν•¨ν•œ λͺ¨λ“  κ°’ νƒ€μž…μ€, κ°’ νƒ€μž…μ„ μ†Œμœ ν•œ 엔티티에 생λͺ…μ£ΌκΈ°λ₯Ό μ˜μ‘΄ν•¨ (μ€‘μš”!)

 

λ§Œμ•½μ—, ν•œ μ—”ν‹°ν‹°μ—μ„œ 같은 κ°’ νƒ€μž…μ„ μ‚¬μš©ν•˜λ©΄? -> @AttributeOverride : 속성 μž¬μ •μ˜

컬럼 λͺ…이 쀑볡이 λœλ‹€.

-> @AttributeOverrides, @AttributeOverride λ₯Ό μ‚¬μš©ν•΄μ„œ 컬럼 λͺ… 속성을 μž¬μ •μ˜

@Entity
public class Member {

	..
    @Embedded
    private Address homeAddress;
    
    // λ§Œμ•½, workAddress도 λ§Œλ“€κ³  μ‹Άλ‹€!! μ•„λž˜ μ½”λ“œ μ°Έκ³ !
    @AttributeOverrides ( {@AttributeOverride(name = "city",
                          column = @Column(name = "WORK_CITY")),
                          @AttributeOverride(name = "street",
                          column = @Column(name = "WORK_STREET")),
                          @AttributeOverride(name = "zipcode",
                          column = @Column(name = "WORK_ZIPCODE")) })
	private Address workAddress;
    
    ..
}

 

 

λ°˜μ‘ν˜•