1.Many-to-Many映射(多对多映射关系)
多对多通常是通过建立一张中间表,然后由任一一个多的一方来维护关联关系.
2.一方:
@ManyToMany(mappedBy="teachers") ---> 表示由另一方来进行维护
3另一方:
@ManyToMany ---> ManyToMany指定多对多的关联关系
@JoinTable(name="t_teacher_course",joinColumns={ @JoinColumn(name="stuid")}, inverseJoinColumns={ @JoinColumn(name = "tid") }) ---> 因为多对多之间会通过一张中间表来维护两表直接的关系,所以通过 JoinTable 这个注解来声明,name就是指定了中间表的名字,JoinColumns是一个 @JoinColumn类型的数组,表示的是我这方在对方中的外键名称,inverseJoinColumns也是一个 @JoinColumn类型的数组,表示的是对方在我这放中的外键名称.
(1)这是hibernate.cfg.xml中的内容
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 显示sql语句 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 启动 -->
<property name="show_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!-- 获得链接-->
<!--test是数据库的名字 characterEncoding=utf-8是语言 -->
<property name="hibernate.connection.url">jdbc:mysql:///test?characterEncoding=utf-8</property>
<property name="hibernate.connection.username">root</property> <!-- 用户名 -->
<property name="hibernate.connection.password"></property> <!-- 密码 -->
<!--实体类配置文件的路径 -->
<mapping resource="pojo/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(2)创建pojo对象,并在同一个包中创建对应的配置文件,命名为pojo名称
package com.oracle.pojo;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
@Id
private int stuId;
private String stuName;
@ManyToMany
@JoinTable(name="student_teacher",joinColumns={@JoinColumn(name="tid")},inverseJoinColumns={@JoinColumn(name="stuid")})
private List<Teacher> teachers;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int stuId, String stuName) {
super();
this.stuId = stuId;
this.stuName = stuName;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(List<Teacher> teachers) {
this.teachers = teachers;
}
@Override
public String toString() {
return "Student [stuId=" + stuId + ", stuName=" + stuName + "]";
}
}
package com.oracle.pojo;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="teacher")
public class Teacher {
@Id
private int tId;
private String tName;
@ManyToMany(mappedBy="teachers")
private List<Student> students;
public int gettId() {
return tId;
}
public void settId(int tId) {
this.tId = tId;
}
public String gettName() {
return tName;
}
public void settName(String tName) {
this.tName = tName;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public Teacher(int tId, String tName) {
super();
this.tId = tId;
this.tName = tName;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Teacher [tId=" + tId + ", tName=" + tName + ", students=" + students + "]";
}
}
增加
package com.oracle.teat;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.oracle.pojo.Student;
import com.oracle.pojo.Teacher;
import com.oracle.until.Until;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session=Until.getSession();
Transaction transaction=session.beginTransaction();
Teacher teacher = new Teacher(101, "王老师");
Teacher teacher2 = new Teacher(102, "数学老师");
Student student1= new Student(2001, "张三");
Student student2= new Student(2002, "李四");
Student student3= new Student(2003, "王力宏");
Student student4= new Student(2004, "赵四");
Student student5= new Student(2005, "刘能");
Student student6= new Student(2006, "谢广坤");
List<Teacher> teachers = new ArrayList<>();
teachers.add(teacher);
teachers.add(teacher2);
student1.setTeachers(teachers);
student2.setTeachers(teachers);
student3.setTeachers(teachers);
student4.setTeachers(teachers);
student5.setTeachers(teachers);
student6.setTeachers(teachers);
//保存老师
session.save(teacher);
session.save(teacher2);
//保存学生
session.save(student1);
session.save(student2);
session.save(student3);
session.save(student4);
session.save(student5);
session.save(student6);
transaction.commit();
Until.closeSession();
}
}
查询
package com.oracle.teat;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.oracle.pojo.Student;
import com.oracle.pojo.Teacher;
import com.oracle.until.Until;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session=Until.getSession();
Transaction transaction=session.beginTransaction();
Teacher teacher= session.load(Teacher.class, 101);
System.out.println(teacher);
transaction.commit();
Until.closeSession();
}
}