以Classes[班级]和Student[学生]为例的Demo
Classes .java public class Classes implements Serializable {
private long Cid;
private String Cname;
private String Cdesc; private Set<Student> students;
//get和set
}
Student .java public class Student implements Serializable {
private long Sid;
private String Sname;
private String Sdesc;
//get和set
}
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.test.test.Classes" table="classes">
<id name="Cid" type="java.lang.Long" length="5">
<column name="Cid"></column>
<generator class="increment"></generator>
</id>
<property name="Cname" type="java.lang.String" length="20"></property>
<property name="Cdesc" type="java.lang.String" length="50"></property> <!--
set元素和类中的set集合对应
* 从外键的角度建立classes与student之间的关系
* 从类与类的角度建立classes与student之间的关系
cascade级联
inverse是用来维护关系的
* 要么是一对多的关系
* 要么多对多的关系
* 谁来维护关系
inverse所在的映射文件对应的持久化对象维护关系
默认值是false 表明维护关系
true 表明不维护关系
-->
<set name="students" cascade="all">
<!--
key是用来描述外键
column就代表外键的名字
-->
<key>
<column name="Cid"></column>
</key>
<!--
classes和哪个对象进行关联
-->
<one-to-many class="cn.test.test.Student"/>
</set> </class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.test.test.Student" table="student">
<id name="Sid" type="java.lang.Long" length="5">
<column name="Sid"></column>
<generator class="increment"></generator>
</id>
<property name="Sname" type="java.lang.String" length="20"></property>
<property name="Sdesc" type="java.lang.String" length="50"></property>
</class>
</hibernate-mapping>
package cn.test.test; import java.util.HashSet;
import java.util.List;
import java.util.Set; import javax.crypto.interfaces.PBEKey; 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("Tom");
classes.setCdesc("1234567890-");
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 student=new Student();
student.setSname("屌丝班");
student.setSdesc("都是纯逗比纯屌丝");
session.save(student); Classes classes=new Classes();
classes.setCname("Tom");
classes.setCdesc("1234567890-");
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, 1L);
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, 1L);
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, 1L);
Classes classes=(Classes)session.get(Classes.class, 1L);
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, 1L);
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, 1L);
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, 1L);
session.delete(classes); transaction.commit();
session.close();
}
}
一对多单向关联例子