'component'에 해당되는 글 1건

  1. 2010.06.01 Hibernate의 컴포넌트
EMOPLOYEE 테이블이 스키마가 다음과 같을때
 EMPLOYEE
 ID
 KOR_SURNAME
 KOR_NAME
 ENG_SURNAME
 ENG_NAME

<component> 태크로 컴포넌트 매핑하기

<class name="Employee" table="EMPLOYEE">
  <id name="id" column="ID" type="int">
    <generator class="identity" />
  </id>

  <component name="korName" class="Name">
    <property name="surname" column="KOR_SURNAME" />
    <property name="name" column="KOR_NAME" />
  </component>

  <component name="engName" class="Name">
    <property name="surname" column="ENG_SURNAME" />
    <property name="name" column="ENG_NAME" />
 </component>
</class>
<component>의 name은 프로퍼티의 이름,  class는 값 타입으로 사용되는 클래스를 의미.
korName프로퍼티의 surname프로퍼티는  KOR_SURNAME칼럼과 매핑된다.

<component>로  매핑되는 클래스도 기본 생성자를 제공해야 함
public class Name {
  private String surname;
  private String name;

  public Name(String surname, String name) {
    this.surname = surname;
    this.name = name;
  }

  public Name() {}

  public void setName() ....

  public String getSurname() ....

  ..
}

example
Employee emp = new Employee();
Name korName = new Name("박", "지성");
Name engName = new Name("Park", "Jisung");
emp.setKorName(korName);
emp.setEngName(engName);
session.save(emp);

Employee emp = (Employee)session.get(Employee.clss, new Integer(1));
Name korName = emp.getKorName();
korName.getSurName();
....

컴포넌트에서 부모객체 참조하기

public class Name {
  private Employee employee;
 
  public Employee getEmployy() {
    return employee;
  }

  public void setEmployee(Employee employee) {
    this.employee = empoyee;
  }

  ..
}

<parent> 로 부모객체를 명시
<class name="Employee" table="EMPLOYEE">
  ..
  <component name="forName" class="Name">
    <parent name="employee" />
    <property name="surname" column="KOR_SURNAME" />
    ..
  </component>
</class>

example
Employee emp = (Employee) session.get(Employee.class, new Integer(1));
Name korName = emp.getKorName();
..
korName.getEmployee();

컴포넌트 중첩
<class name="Employee" table="EMPLOYEE">
  ..
  <component name="korName" class="Name">
    <property name="surName" column="KOR_SURNAME" />
    <property name="name" column="KOR_NAME" />
  
    <component name="familyInfo" class="FamilyInfo">
      <property name="origin" column="FAMILY_ORIGIN" />
      ..
    </component>
  </component>
</class>

 

저작자 표시
신고
Posted by 영겁회귀

티스토리 툴바