第一步:编写两个pojo,比如一个学生表一个课程表  这里使用注解。

需要

课程表:

 package com.qcf.pox;

 import java.util.HashSet;
import java.util.Set; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany; @Entity
public class Course {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@ManyToMany(mappedBy="courses")
private Set<Student2> student2s=new HashSet<Student2>();
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;
}
public Set<Student2> getStudent2s() {
return student2s;
}
public void setStudent2s(Set<Student2> student2s) {
this.student2s = student2s;
}
public Course(int id, String name, Set<Student2> student2s) {
super();
this.id = id;
this.name = name;
this.student2s = student2s;
}
public Course() {
super();
} }

学生表:

 package com.qcf.pox;

 import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany; @Entity
public class Student2 {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@ManyToMany(cascade=CascadeType.ALL)
private Set<Course> courses=new HashSet<Course>();
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;
}
public Set<Course> getCourses() {
return courses;
}
public void addCourses(Course courses) {
this.courses.add(courses);
}
public Student2(int id, String name, Set<Course> courses) {
super();
this.id = id;
this.name = name;
this.courses = courses;
}
public Student2() {
super();
} }

第二步:在hibernate.cfg.xml文件中引入这两个po类

         <mapping class="com.qcf.pox.Student2"/>
<mapping class="com.qcf.pox.Course"/>

第三步:编写测试代码

 package com.qcf.test;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration; import com.qcf.pox.Course;
import com.qcf.pox.Student2; public class TestManyToMany {
public static void main(String[] args) {
Configuration configuration=new AnnotationConfiguration().configure();
SessionFactory factory=configuration.buildSessionFactory();
Session session=factory.openSession();
Transaction transaction=session.beginTransaction(); //创建两个课程
Course course=new Course();
course.setName("java");
Course course2=new Course();
course2.setName("C#"); //创建两个学生
Student2 student=new Student2();
student.setName("zhangsan");
Student2 student2=new Student2();
student2.setName("lisi"); student.addCourses(course);
student.addCourses(course2); student2.addCourses(course);
student2.addCourses(course2); session.save(student);
session.save(student2); transaction.commit();
session.close(); } }

执行成功后的结果:

Hibernat之关系的处理多对多-LMLPHPHibernat之关系的处理多对多-LMLPHPHibernat之关系的处理多对多-LMLPHP

05-11 15:04