[[Hibernateの覚書]]

* 多対多関連 [#m0e9f03c]
#contents
** 1. テーブル [#d519424a]
** 1.概要 [#r880261e]
テーブルSTUDENTとSUBJECTの多対多関連を~
関連テーブルSTUDENT_SUBJECTを使用して実現する。

** 2. テーブル [#d519424a]
*** STUDENTテーブル [#c97a30d0]
 Name    Null     Type                     
 ---------- -------- ------------------------
 ID         NOT NULL NUMBER(38)
 NAME       NOT NULL VARCHAR2(100)

*** SUBJECTテーブル [#x5659c3b]
 Name    Null     Type                     
 ---------- -------- ------------------------
 ID         NOT NULL NUMBER(38)
 SUBJECT    NOT NULL VARCHAR2(100)

*** STUDENT_SUBJECTテーブル [#ga79504b]
 Name    Null     Type                     
 ---------- -------- ------------------------
 STUDENT_ID NOT NULL NUMBER(38)
 SUBJECT_ID NOT NULL NUMBER(38)

** 3.マッピング [#wd183d27]
*** Student.hbm.xml [#nc971e15]

 <?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 entity-name="Student" name="com.ziqoo.entity.Student" table="STUDENT">
        <id name="id" type="big_decimal">
            <column name="ID" precision="38" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="100" not-null="true" />
        </property>
        <!-- 多対多関連 -->
        <set fetch="join" name="subjects" table="STUDENT_SUBJECT">
            <key column="STUDENT_ID"/>
            <many-to-many entity-name="Subject" column="SUBJECT_ID"/>
        </set>
    </class>
 </hibernate-mapping>

*** Subject.hbm.xml [#p52c07c9]
 <?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 entity-name="Subject" name="com.ziqoo.entity.Subject" table="SUBJECT">
        <id name="id" type="big_decimal">
            <column name="ID" precision="38" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="subject" type="string">
            <column name="SUBJECT" length="100" not-null="true" />
        </property>
    </class>
 </hibernate-mapping>

** 4.マッピング対象クラス [#x0716453]
*** Student.java [#zb4abbb5]
 package com.ziqoo.entity;
 import java.math.BigDecimal;
 import java.util.Set;
 public class Student  implements java.io.Serializable {
     private BigDecimal id;
     private String name;
     private Set<Subject> subjects;
 ...setter、getterは省略...
 }
*** Subject.java [#ne1843a3]
 package com.ziqoo.entity;
 import java.math.BigDecimal;
 public class Subject  implements java.io.Serializable {
     private BigDecimal id;
     private String subject;
  ...setter、getterは省略...
  }



トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS