λ°μν
μλ° ORM νμ€ JPA νλ‘κ·Έλλ° - κΈ°λ³ΈνΈμ 곡λΆνλ©° μ 리ν λ΄μ©μ λλ€.
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;
..
}
λ°μν