Hibernate - 複数テーブルのマッピング(1対1)Advertisementone-to-one
複数のテーブルを結合して問い合わせを行う場合、Hibernateマッピングファイルにone-to-one要素を記述する。
サンプルテーブル以下の2つのテーブルを利用する。SKILL表
SKILL_INFORMATION表
DTO(永続化クラス)SkillDto.java
package dto;
public class SkillDto {
private Integer id;
private String text;
private SkillInformationDto skillInformationDto;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public SkillInformationDto getSkillInformationDto() {
return skillInformationDto;
}
public void setSkillInformationDto(SkillDto skillInformationDto) {
this.skillInformationDto = skillInformationDto;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
SkillInformationDto.java
package dto;
public class SkillInformationDto {
private Integer id;
private String text;
private SkillDto skillDto;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public SkillDto getSkillDto() {
return skillDto;
}
public void setSkillDto(SkillDto skillDto) {
this.skillDto = skillDto;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
SkillDto, SkillInformationDto共に、対応する相手のプロパティを保持している。
マッピング情報skill.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="dto.SkillDto" table="skill">
<id name="id" column="id">
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String" column="name"/>
<one-to-one class="dto.SkillInformationDto"
name="skillInformationDto"
access="field"
cascade="all"
outer-join="auto"/>
</class>
</hibernate-mapping>
skillInformation.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="dto.SkillInformationDto" table="skill_information">
<id name="id" column="id">
<generator class="assigned" />
</id>
<property name="text" type="java.lang.String" column="text"/>
<one-to-one class="dto.SkillDto"
name="skillDto"
access="field"
cascade="all"
outer-join="auto"/>
</class>
</hibernate-mapping>
outer-joinプロパティがtrueの場合、外部結合による問い合わせが実行される。
クライアントOneToOne.java
package client;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import dto.SkillDto;
import dto.SkillInformationDto;
public class OneToOne{
public static void main(String args[]) throws Exception{
Configuration config = new Configuration().configure();
SessionFactory sessionfactory = config.buildSessionFactory();
Session session = sessionfactory.openSession();
List list = session.createCriteria(SkillDto.class).list();
for(int i = 0; i < list.size(); i++){
SkillDto skill = (SkillDto)list.get(i);
System.out.print(skill.getText() + "\t");
SkillInformationDto skillInformationDto = skill.getSkillInformationDto();
System.out.println(skillInformationDto.getText());
}
}
}
はじめにSkillDtoをHibernate経由で取得する。マッピング情報が定義されているのでSkillDtoは対応するSkillInformationDtoクラスのオブジェクトを保持している。実行結果 java じゃば .NET どっとねっと PHP ぴーえいちぴー Advertisement |
ショートカット・634トップページ・このカテゴリのトップページに戻る ・634ラボ サイト検索Y!ログール |