1.
示例代码:
Student.java
package cn.itcast.many2many;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("serial")
public class Student implements java.io.Serializable {
private Integer id;
private String name;
private Set<Course> courses=new HashSet(0);
public Integer getId() {
return id;
}
public void setId(Integer 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 setCourses(Set<Course> courses) {
this.courses = courses;
}
}
Course.java
package cn.itcast.many2many;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("serial")
public class Course implements java.io.Serializable{
private Integer id;
private String name;
private Set<Student> studentes=new HashSet(0);
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudentes() {
return studentes;
}
public void setStudentes(Set<Student> studentes) {
this.studentes = studentes;
}
}
App.java
package cn.itcast.many2many;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class App {
private static SessionFactory sf=null;
static{
Configuration config=new Configuration();
config.configure("cn/itcast/many2many/hibernate.cfg.xml");
config.addClass(Student.class);
config.addClass(Course.class);
sf=config.buildSessionFactory();
}
/*
* 知识点4:测试保存
*/
@Test
public void saveStudentAndCoure(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Student s1=new Student();
s1.setName("风侠");
Student s2=new Student();
s2.setName("雨侠");
Course c1=new Course();
c1.setName("语文");
Course c2=new Course();
c2.setName("数学");
//建立关联关系
s1.getCourses().add(c1);
s1.getCourses().add(c2);
s2.getCourses().add(c1);
s2.getCourses().add(c2);
c1.getStudentes().add(s1);
c1.getStudentes().add(s2);
c2.getStudentes().add(s1);
c2.getStudentes().add(s2);
session.save(s1);
session.save(s2);
session.save(c1);
session.save(c2);
tx.commit();
session.close();
}
//知识点5:解除1号学生和1号课程的关联关系
@Test
public void removeMany2Many(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
//查询1号学生
Student s1=(Student)session.get(Student.class, 1);
//查询1号课程
Course c1=(Course)session.get(Course.class, 1);
//解除关联关系
s1.getCourses().remove(c1);
c1.getStudentes().remove(s1);
tx.commit();
session.close();
}
//知识点6:改变1号学生和2号课程的关联关系,改为1号学生和1号课程
@Test
public void changeMany2Many(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
//查询1号学生
Student s1=(Student)session.get(Student.class, 1);
//查询1号课程
Course c1=(Course)session.get(Course.class, 1);
//查询2号课程
Course c2=(Course)session.get(Course.class, 2);
//解除1号学生和2号课程关联关系
s1.getCourses().remove(c2);
c2.getStudentes().remove(s1);
//建立1号学生和1号课程的关联关系
s1.getCourses().add(c1);
c1.getStudentes().add(s1);
tx.commit();
session.close();
}
//知识点7:删除2号学生,产生异常
@Test
public void removeStudent(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
//查询2号学生
Student s2=(Student)session.get(Student.class, 2);
session.delete(s2);
tx.commit();
session.close();
}
/*
* 知识点8:删除1号课程.这里能删除,因为课程是主控方法 student.hbm.xml配置inverse="true"
* * 能删除1号课程
* * 能删除1号课程对应的中间表信息,但不能是删除学生表的信息
*/
@Test
public void removeCourse(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
//查询1号课程
Course c1=(Course)session.get(Course.class, 1);
session.delete(c1);
tx.commit();
session.close();
}
//知识点9:删除1号课程的同时,要把1号和2号学生删掉? cascade="delete", 中间表用到的学生应先删除
@Test
public void removeCourseCase(){
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
//查询1号课程
Course c1=(Course)session.get(Course.class, 1);
session.delete(c1);
tx.commit();
session.close();
}
}
Student.hbm.xml
Course.hbm.xml
hibernate.cfg.xml