annotation方式:
一。编写多方实体类:
@Entity
public class Student{
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
二。编写多方实体类
@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"/>
四。测试结果
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 (teacher_Id, student_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)
xml方式:
一。编写如上实体类
二。配置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="org.hibernate.tutorial.domain">
<class name="Student" >
<id name="id" column="studentId">
<generator class="native"/>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
此方不用特殊配置
三。配置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="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>
配置本类的属性students,table是配置中间表的表名,key是配置中间表列指向(外键)本类ID,many-to-many配置映射类,及中间表列指向(外键)对方表ID
四。设置hibernate.cfg.xml
<mapping resource="org/hibernate/tutorial/domain/Teacher.hbm.xml"/>
<mapping resource="org/hibernate/tutorial/domain/Student.hbm.xml"/>