以下通过一个案例来讲解hirbernate多对多的应用
通过一个持久化类Student,和一个持久化类Course类
以下是二者的Student.hbm.xml和Course.hbm.xml
Student.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Course.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
持久化类Student
package hibernatetest2;
import hibernatetest3.Course;
import java.util.HashSet;
import java.util.Set;
/**
* Student entity. @author MyEclipse Persistence Tools
*/
public class Student implements java.io.Serializable {
// Fields
private Integer sid;
private String sname;
private String sinfo;
private Integer cid;
private Classes classes;
//设置一方的Set
private Set
// Constructors
public Set
return course;
}
public void setCourse(Set
this.course = course;
}
/** default constructor */
public Student() {
}
/** full constructor */
public Student(String sname, String sinfo, Integer cid) {
this.sname = sname;
this.sinfo = sinfo;
this.cid = cid;
}
// Property accessors
public Integer getSid() {
return this.sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return this.sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSinfo() {
return this.sinfo;
}
public void setSinfo(String sinfo) {
this.sinfo = sinfo;
}
public Integer getCid() {
return this.cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", sinfo=" + sinfo
+ ", cid=" + cid + "]";
}
}
Course持久化类
package hibernatetest3;
import java.util.HashSet;
import java.util.Set;
/**
* Course entity. @author MyEclipse Persistence Tools
*/
public class Course implements java.io.Serializable {
// Fields
private Integer csid;
private String cname;
private String cinfo;
private Integer cscore;
//设置多方的Set
private Set
// Constructors
public Set
return student;
}
public void setStudent(Set
this.student = student;
}
/** default constructor */
public Course() {
}
/** full constructor */
public Course(String cname, String cinfo, Integer cscore) {
this.cname = cname;
this.cinfo = cinfo;
this.cscore = cscore;
}
// Property accessors
public Integer getCsid() {
return this.csid;
}
public void setCsid(Integer csid) {
this.csid = csid;
}
public String getCname() {
return this.cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCinfo() {
return this.cinfo;
}
public void setCinfo(String cinfo) {
this.cinfo = cinfo;
}
public Integer getCscore() {
return this.cscore;
}
public void setCscore(Integer cscore) {
this.cscore = cscore;
}
@Override
public String toString() {
return "Course [csid=" + csid + ", cname=" + cname + ", cinfo=" + cinfo
+ ", cscore=" + cscore + ", student=" + student + "]";
}
}
测试类TestCourse
package hibernatetest3;
import hibernatetest2.Student;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestCourse {
private Configuration config;
private SessionFactory sessionFactory;
private Session session;
private Transaction tx;
@Before
public void init(){
config=new Configuration().configure();
sessionFactory=config.buildSessionFactory();
session=sessionFactory.openSession();
tx=session.beginTransaction();
}
@After
public void destory(){
tx.commit();
session.close();
}
//查询指定学生选择的课程
@Test
public void searchTest(){
//查找出学生id为2选择的所有的课程
// Student stu=(Student)session.get(Student.class,2);
// Set set=stu.getCourse();
// Iterator
// while(it.hasNext()){
// System.out.println(it.next());
// }
//指定课程id 根据课程查找出有哪些学生选择该课程
Course cur=(Course)session.get(Course.class,2);
Set set=cur.getStudent();
Iterator
while(it.hasNext()){
System.out.println(it.next());
}
}
@Test
public void addTest(){
// //构造学生,进行选课
// Student stu=new Student("张超","信管一班",2);
// //选择出两个课程,然后进行选择
// Course course2=(Course)session.get(Course.class,2);
// Course course3=(Course)session.get(Course.class,3);
// stu.getCourse().add(course3);
// stu.getCourse().add(course2);
// session.save(stu);
//新增课程,由学生进行选课
Course cur=new Course("Struts2","很难",6);
//查找两个学生,然后将课程放入学生
Student stu1=(Student)session.get(Student.class,1);
Student stu2=(Student)session.get(Student.class,2);
stu1.getCourse().add(cur);
stu2.getCourse().add(cur);
session.save(cur);
}
@Test//学生编号为2的学生选课选错了,需要修改
public void updateTest(){
//先找到学生ID为2的学生
Student stu2=(Student)session.get(Student.class,2);
//再找到已经选择错了的课,和即将要选择的课程
Course cur5=(Course)session.get(Course.class,6);
Course cur6=(Course)session.get(Course.class,5);
stu2.getCourse().remove(cur5);
stu2.getCourse().add(cur6);
session.update(stu2);//需要在学生所对应的类里面将inverse改写成false,即表示Student类自身进行更改
}
@Test
public void deleteTest(){
//删除编号为2的学生所选的课,先找到编号为2的学生
Student stu2=(Student)session.get(Student.class,4);
session.delete(stu2);//如果选择delete,则会级联删除与之关联的所有对象
}
}
注意在打印输出Student类的对象时,不能再Student类里面的toString()方法和Course类的toString()方法里同时写入Course对象属性和Student对象属性,否则会进入一个死循环。