マルチペアマルチ双方向関連(annotation&xml)

5542 ワード

annotation方式:
一.エンティティクラスを作成し、双方向の関連付けは双方に相手のコンテナを入れるべきである.

@Entity
public class Student{
	private int id;
	private String name;
	private Set<Teacher> students =new HashSet<Teacher>();
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	@ManyToMany(mappedBy="students")
	public Set<Teacher> getStudents() {
		return students;
	}

	public void setStudents(Set<Teacher> students) {
		this.students = students;
	}

	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
}


注意mappedByを構成し、相手クラスの属性を主導する
二.
@Entity
public class Teacher {
	private int id;
	private String name;
	private Set<Student> students =new HashSet<Student>();
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	@ManyToMany
	@JoinTable(name="t_s",
	  joinColumns={@JoinColumn(name="teacher_id")},
	  inverseJoinColumns={@JoinColumn(name="student_id")}
	)
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
	
	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
}

コメント@ManyToManyを設定し、@JoinTableは中間テーブルを設定し、nameは名前を設定し、joinColumnsは中間テーブル列が現在のクラスIDを指し、inverseJoinColumnsは中間テーブル列が相手のクラスIDを指すように設定する
三.hibernateを設定します.cfg.xml

<mapping class="org.hibernate.tutorial.domain.Teacher"/>
<mapping class="org.hibernate.tutorial.domain.Student"/>

xml方式:
双方xmlファイルの構成

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="org.hibernate.tutorial.domain"> 

    <class name="Teacher" > 
        <id name="id" column="teacherId"> 
            <generator class="native"/> 
        </id> 
        <property name="name"></property>
        <set name="students" table="t_s">
        	<key column="teacher_Id"></key>
        	<many-to-many class="org.hibernate.tutorial.domain.Student" column="student_Id"/>
        </set>
    </class> 

</hibernate-mapping> 

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="org.hibernate.tutorial.domain"> 

    <class name="Student" > 
        <id name="id" column="studentId"> 
            <generator class="native"/> 
        </id> 
        <property name="name"></property>
        <set name="students" table="t_s">
        	<key column="student_Id"></key>
        	<many-to-many class="org.hibernate.tutorial.domain.Student" column="teacher_Id"/>
        </set>
    </class> 

</hibernate-mapping> 

双方は集合属性を構成し、nameは本クラス属性を構成し、tableは中間テーブル名を構成し(双方が一致しなければならない.そうしないと2つのテーブルが生成される)、keyは中間テーブル列が本クラスIDを指す.
many-to-many構成はクラスを含み、column構成中間テーブル列は相手テーブルIDを指す
テスト結果


    create table Student (
        studentId integer not null auto_increment,
        name varchar(255),
        primary key (studentId)
    )

    create table Teacher (
        teacherId integer not null auto_increment,
        name varchar(255),
        primary key (teacherId)
    )

    create table t_s (
        teacher_Id integer not null,
        student_Id integer not null,
        primary key (student_Id, teacher_Id)
    )

    alter table t_s 
        add index FK_t2s4kp3kjctswb718yn0ny9ck (student_Id), 
        add constraint FK_t2s4kp3kjctswb718yn0ny9ck 
        foreign key (student_Id) 
        references Student (studentId)

    alter table t_s 
        add index FK_q8pyhjtlr4wxothocdby56ttk (teacher_Id), 
        add constraint FK_q8pyhjtlr4wxothocdby56ttk 
        foreign key (teacher_Id) 
        references Teacher (teacherId)

    alter table t_s 
        add index FK_q8pyhjtlr4wxothocdby56ttk (teacher_Id), 
        add constraint FK_q8pyhjtlr4wxothocdby56ttk 
        foreign key (teacher_Id) 
        references Student (studentId)