특징

  • Table Per Class Hierachy 또는 Table Per Subclass를 사용한 상속구조로 맵핑 했다면, 다음과 같이 해도 아무일 없다.
저장하기
CreditCard cc = new CreditCard();
cc.setNumber(ccNumber);
cc.setType(ccType);
cc.setExpMonth(...);
cc.setExpYear(...);
User user = (User) session.get(User.class, userId);
// Call convenience method that sets both sides of the association
user.addBillingDetails(cc);
// Complete unit of work
가져오기
User user = (User) session.get(User.class, userId);
for( BillingDetails bd : user.getBillingDetails() ) {
// Invoke CreditCard.pay() or BankAccount.pay()
bd.pay(paymentAmount);
}
  • @ManyToMany 와 @OneToMany의 기본 FetchMode는 LAZY다. 따라서, 위의 코드에서 이터레이터로 BillingDetails를 하나 가리키기 시작할 때 콜렉션을 가져온다.
  • 루프 돌지 않고, user.getBillingDetails() 호출하면, 쿼리는 날아가지 않는다.