package com.hibernate1.study;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class DoTest {
/**
* 1、保存班级
* 2、保存学生
* 3、保存班级的时候同时保存学生
* 4、保存班级的时候同时保存学生,并且建立班级和学生之间的关系
* 5、已经存在一个班级,新建一个学生,并且建立该学生和该班级之间的关系
* 6、已经存在一个学生,新建一个班级,并且建立该学生和该班级之间的关系
* 7、已经存在一个学生,已经存在一个班级,解除该学生和原来班级之间的关系,建立该学生和新班级之间的关系
* 8、已经存在一个学生,解除该学生和该学生所在班级之间的关系
* 9、解除该班级和所有的学生之间的关系,再重新建立该班级和一些新的学员之间的关系
* 10、解除该班级和所有的学生之间的关系
* 11、删除班级
* *
* * 解除该班级和所有的学生之间的关系
* * 删除该班级
* *
* 删除班级的同时删除学生
* 12、删除学生
* 同删除班级
*/
private static SessionFactory sessionFactory;
static{
Configuration configuration=new Configuration();
configuration.configure();
sessionFactory=configuration.buildSessionFactory();
}
// //1、保存班级
// @Test
// public void testSaveClasses(){
// Session session=sessionFactory.openSession();
// Transaction transaction= session.beginTransaction();
// Classes classes=new Classes();
// classes.setCname(“张”);
// classes.setCdesc(“123”);
// session.save(classes);
// transaction.commit();
// session.close();
// }
// //2、保存学生
// @Test
// public void testSaveStudents(){
// Session session=sessionFactory.openSession();
// Transaction transaction= session.beginTransaction();
// Student student=new Student();
// student.setSname(“你好”);
// student.setSdesc(“一起来”);
// session.save(student);
// transaction.commit();
// session.close();
// }
//3、保存班级的时候同时保存学生
/*
* Hibernate: select max(Sid) from student
Hibernate: select max(Cid) from classes
Hibernate: insert into student (Sname, Sdesc, Sid) values (?, ?, ?)
Hibernate: insert into classes (Cname, Cdesc, Cid) values (?, ?, ?)
*/
@Test
public void testSaveClasses_Students(){
Session session=sessionFactory.openSession();
Transaction transaction= session.beginTransaction();
Student student1=new Student();
student1.setSname(“李华”);
student1.setSdesc(“大二学生”);
Student student2=new Student();
student2.setSname(“小明”);
student2.setSdesc(“大二学生”);
Classes classes=new Classes();
classes.setCname(“软件工程2016级01班”);
classes.setCdesc(“编程语言”);
//设置关联关系
Set<Student> students=new HashSet<Student>();
students.add(student1);
students.add(student2);
classes.setStudents(students);
session.save(classes);
transaction.commit();
session.close();
}
//4、保存班级的时候同时保存学生,并且建立班级和学生之间的关系
/*classes.hbm.xml中设置;<set name=”students” cascade=”save-update”>
* Hibernate: select max(Cid) from classes
Hibernate: select max(Sid) from student
Hibernate: insert into classes (Cname, Cdesc, Cid) values (?, ?, ?)
Hibernate: insert into student (Sname, Sdesc, Sid) values (?, ?, ?)
Hibernate: update student set Cid=? where Sid=?
保存班级的时候同时保存学生,并且建立班级和学生之间的关系
* 在classes.hbm.xml文件中
* <set name=”students” cascade=”save-update”>
* inverse的值为默认或者false,才能让classes维护student的关系
设置<set name=”students” cascade=”save-update” inverse=”true”>
Hibernate: select max(Cid) from classes
Hibernate: select max(Sid) from student
Hibernate: insert into classes (Cname, Cdesc, Cid) values (?, ?, ?)
Hibernate: insert into student (Sname, Sdesc, Sid) values (?, ?, ?)
不建立关联
*/
// @Test
// public void testSaveClasses_R_Students(){
// Session session=sessionFactory.openSession();
// Transaction transaction= session.beginTransaction();
//
// Student student=new Student();
// student.setSname(“Tom”);
// student.setSdesc(“1234567890-“);
//
// //session.save(student);必须删除掉
//
// Set<Student> students=new HashSet<Student>();
// students.add(student);
//
// Classes classes=new Classes();
// classes.setCname(“屌丝班”);
// classes.setCdesc(“都是纯逗比纯屌丝”);
//
// classes.setStudents(students);
//
// session.save(classes);
//
// transaction.commit();
// session.close();
// }
//
//
// //5、已经存在一个班级,新建一个学生,并且建立该学生和该班级之间的关系
// /**
// * Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?
// Hibernate: select students0_.Cid as Cid1_1_, students0_.Sid as Sid1_, students0_.Sid as Sid0_0_, students0_.Sname as Sname0_0_, students0_.Sdesc as Sdesc0_0_ from student students0_ where students0_.Cid=?
// Hibernate: select max(Sid) from student
// Hibernate: insert into student (Sname, Sdesc, Sid) values (?, ?, ?)
// Hibernate: update student set Cid=? where Sid=?
// */
// @Test
// public void testSaveClassesAndStudents_R(){
// Session session=sessionFactory.openSession();
// Transaction transaction=session.beginTransaction();
// Classes classes=(Classes) session.get(Classes.class, new Integer(1));
// Student student=new Student();
// student.setSname(“Lucy”);
// student.setSdesc(“qwertyuiop”);
// Set<Student> set=new HashSet<Student>();
// //建立关系
// classes.getStudents().add(student);
//
// //classes.setStudents(set); 把班级中的学生重新更新了
//
// transaction.commit();
// session.close();
// }
// //6、已经存在一个学生,新建一个班级,并且建立该学生和该班级之间的关系
// /*
// * Hibernate: select student0_.Sid as Sid0_0_, student0_.Sname as Sname0_0_, student0_.Sdesc as Sdesc0_0_ from student student0_ where student0_.Sid=?
// Hibernate: select max(Cid) from classes
// Hibernate: insert into classes (Cname, Cdesc, Cid) values (?, ?, ?)
// Hibernate: update student set Cid=? where Sid=?
// */
// @Test
// public void testSaveStudentAndClasses_R(){
// Session session =sessionFactory.openSession();
// Transaction transaction= session.beginTransaction();
//
// Student student=(Student) session.get(Student.class, 2L);
//
// Classes classes=new Classes();
// classes.setCname(“文艺青年班”);
// classes.setCdesc(“感觉都是文艺青年棒棒哒”);
//
// Set<Student> set=new HashSet<Student>();
// set.add(student);
// classes.setStudents(set);
//
// session.save(classes);
//
// transaction.commit();
// session.close();
// }
//
// //7、已经存在一个学生,已经存在一个班级,解除该学生和原来班级之间的关系,建立该学生和新班级之间的关系
// /*
// * Hibernate: select student0_.Sid as Sid0_0_, student0_.Sname as Sname0_0_, student0_.Sdesc as Sdesc0_0_ from student student0_ where student0_.Sid=?
// Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?
// Hibernate: select students0_.Cid as Cid1_1_, students0_.Sid as Sid1_, students0_.Sid as Sid0_0_, students0_.Sname as Sname0_0_, students0_.Sdesc as Sdesc0_0_ from student students0_ where students0_.Cid=?
// Hibernate: update student set Cid=? where Sid=?//建立关系
// *
// */
// @Test
// public void testSaveRemove_Student_Classes(){
// Session session=sessionFactory.openSession();
// Transaction transaction=session.beginTransaction();
// Student student=(Student) session.get(Student.class, 2L);
//
// Classes classes=(Classes) session.get(Classes.class, new Integer(1));
// classes.getStudents().add(student);//建立关系,没有必要删除与原来班级的关系,建立关系之后,自动解除与原来班级的关系
// transaction.commit();
// session.close();
// }
// //8、已经存在一个学生,解除该学生和该学生所在班级之间的关系
// /*
// * Hibernate: select student0_.Sid as Sid0_0_, student0_.Sname as Sname0_0_, student0_.Sdesc as Sdesc0_0_ from student student0_ where student0_.Sid=?
// Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?
// Hibernate: select students0_.Cid as Cid1_1_, students0_.Sid as Sid1_, students0_.Sid as Sid0_0_, students0_.Sname as Sname0_0_, students0_.Sdesc as Sdesc0_0_ from student students0_ where students0_.Cid=?
// Hibernate: update student set Cid=null where Cid=? and Sid=?
// *
// */
// @Test
// public void testRemoveStudent(){
// Session session=sessionFactory.openSession();
// Transaction transaction =session.beginTransaction();
//
// Student student=(Student) session.get(Student.class, new Integer(1));
// Classes classes=(Classes)session.get(Classes.class, new Integer(1));
// classes.getStudents().remove(student);
// transaction.commit();
// session.close();
// }
//
// //9、解除该班级和所有的学生之间的关系,再重新建立该班级和一些新的学员之间的关系
// /*
// * Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?
// Hibernate: select student0_.Sid as Sid0_, student0_.Sname as Sname0_, student0_.Sdesc as Sdesc0_ from student student0_ where student0_.Sid in (3 , 4 , 5)
// Hibernate: update student set Cid=null where Cid=?
// Hibernate: update student set Cid=? where Sid=?
// Hibernate: update student set Cid=? where Sid=?
// Hibernate: update student set Cid=? where Sid=?
// *
// */
// @Test
// public void testRemoveAllAndAddSome(){
// Session session =sessionFactory.openSession();
// Transaction transaction= session.beginTransaction();
//
// Classes classes=(Classes) session.get(Classes.class, new Integer(1));
// List<Student> students= session.createQuery(“FROM Student WHERE Sid IN(3,4,5) “).list();
//
// Set<Student> sets=new HashSet<Student>(students);
// classes.setStudents(sets);
//
// transaction.commit();
// session.close();
// }
//
// //10、解除该班级和所有的学生之间的关系
// /*
// * Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?
// Hibernate: update student set Cid=null where Cid=?
// */
// @Test
// public void testRemoveAll(){
// Session session=sessionFactory.openSession();
// Transaction transaction =session.beginTransaction();
//
// Classes classes=(Classes) session.get(Classes.class, new Integer(1));
// classes.setStudents(null);
//
// transaction.commit();
// session.close();
// }
// /*
// * 11、删除班级
// * *
// * * 解除该班级和所有的学生之间的关系
// * * 删除该班级
// * *
// * 删除班级的同时删除学生
// * 12、删除学生
// * 同删除班级
// *
// */
// //11、删除学生,同删除班级
// /*
// * Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?
// Hibernate: select students0_.Cid as Cid1_1_, students0_.Sid as Sid1_, students0_.Sid as Sid0_0_, students0_.Sname as Sname0_0_, students0_.Sdesc as Sdesc0_0_ from student students0_ where students0_.Cid=?
// Hibernate: update student set Cid=null where Cid=?
// Hibernate: delete from student where Sid=?
// Hibernate: delete from student where Sid=?
// Hibernate: delete from student where Sid=?
// Hibernate: delete from classes where Cid=?
// */
// @Test
// public void testDeleteClasses(){
// Session session =sessionFactory.openSession();
// Transaction transaction=session.beginTransaction();
//
// Classes classes=(Classes) session.get(Classes.class, new Integer(1));
// session.delete(classes);
//
// transaction.commit();
// session.close();
}
Hibernate中一对多关联关系保存
最新推荐文章于 2021-01-23 11:50:40 发布