首页 > 代码库 > Hibernate——ORMapping(二)
Hibernate——ORMapping(二)
第八种:多对多单向关联
一、Annotation配置
每个Teacher可以有多个Student,每个Student可以被多个Teacher教
1.Teacher.java
package com.zgy.hibernate.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_teacher")
public class Teacher {
private int id;
private String name;
private Set<Student> students = new HashSet<Student>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@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;
}
}
2.Student.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_student")
public class Student {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.测试,观察结果
Hibernate: create table t_s (teacher_id integer not null, student_id integer not null, primary key (teacher_id, student_id))
Hibernate: create table t_student (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_teacher (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: alter table t_s add constraint FK_3no0wm9bwtogr87t4gvd4t3x5 foreign key (student_id) references t_student (id)
Hibernate: alter table t_s add constraint FK_pj2fmd6skwfi7jm2avlo4n6mf foreign key (teacher_id) references t_teacher (id)
二、Xml配置
1.Teacher.hbm.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="com.zgy.hibernate.model">
<class name="com.zgy.hibernate.model.Teacher" table="t_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="students" table="t_s">
<key column="teacher_id"></key>
<many-to-many class="com.zgy.hibernate.model.Student" column="student_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
2.Student.hbm.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="com.zgy.hibernate.model">
<class name="com.zgy.hibernate.model.Student" table="t_student">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
</class>
</hibernate-mapping>
3.观察结果
Hibernate: create table t_s (teacher_id integer not null, student_id integer not null, primary key (teacher_id, student_id))
Hibernate: create table t_student (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_teacher (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: alter table t_s add constraint FK_3no0wm9bwtogr87t4gvd4t3x5 foreign key (student_id) references t_student (id)
Hibernate: alter table t_s add constraint FK_pj2fmd6skwfi7jm2avlo4n6mf foreign key (teacher_id) references t_teacher (id)
第九种:多对多双向关联
一、Annotation配置
1.Teacher.java
package com.zgy.hibernate.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_teacher")
public class Teacher {
private int id;
private String name;
private Set<Student> students = new HashSet<Student>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@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;
}
}
2.Student.java
package com.zgy.hibernate.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_student")
public class Student {
private int id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(mappedBy="students")
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
3.观察结果:
Hibernate: create table t_s (teacher_id integer not null, student_id integer not null, primary key (teacher_id, student_id))
Hibernate: create table t_student (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_teacher (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: alter table t_s add constraint FK_3no0wm9bwtogr87t4gvd4t3x5 foreign key (student_id) references t_student (id)
Hibernate: alter table t_s add constraint FK_pj2fmd6skwfi7jm2avlo4n6mf foreign key (teacher_id) references t_teacher (id)
二、Xml配置
1.Teacher.hbm.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="com.zgy.hibernate.model">
<class name="com.zgy.hibernate.model.Teacher" table="t_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="students" table="t_s">
<key column="teacher_id"></key>
<many-to-many class="com.zgy.hibernate.model.Student" column="student_id"></many-to-many>
</set>
</class>
</hibernate-mapping>、
2.Student.hbm.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="com.zgy.hibernate.model">
<class name="com.zgy.hibernate.model.Student" table="t_student">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="teachers" table="t_s">
<key column="student_id"></key>
<many-to-many class="com.zgy.hibernate.model.Teacher" column="teacher_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
3.观察结果
Hibernate: create table t_s (teacher_id integer not null, student_id integer not null, primary key (student_id, teacher_id))
Hibernate: create table t_student (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_teacher (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: alter table t_s add constraint FK_3no0wm9bwtogr87t4gvd4t3x5 foreign key (student_id) references t_student (id)
Hibernate: alter table t_s add constraint FK_pj2fmd6skwfi7jm2avlo4n6mf foreign key (teacher_id) references t_teacher (id)
Hibernate——ORMapping(二)