首页 > 代码库 > Hibernate 一对多单向关联Demo

Hibernate 一对多单向关联Demo

以Classes[班级]和Student[学生]为例的Demo

Classes .javapublic class Classes implements Serializable {    private long Cid;    private String Cname;    private String Cdesc;        private Set<Student> students;    //get和set   }
Student .javapublic class Student implements Serializable {    private long Sid;    private String Sname;    private String Sdesc;//get和set}

映射文件:

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5     <class name="cn.test.test.Classes" table="classes"> 6         <id name="Cid" type="java.lang.Long" length="5"> 7             <column name="Cid"></column> 8             <generator class="increment"></generator> 9         </id>10         <property name="Cname" type="java.lang.String" length="20"></property>11         <property name="Cdesc" type="java.lang.String" length="50"></property>12         13         <!-- 14             set元素和类中的set集合对应15               *  从外键的角度建立classes与student之间的关系16               *  从类与类的角度建立classes与student之间的关系17             cascade级联18                 inverse是用来维护关系的19                     *  要么是一对多的关系20                     *  要么多对多的关系21                     *  谁来维护关系22             inverse所在的映射文件对应的持久化对象维护关系23                 默认值是false  表明维护关系24                     true   表明不维护关系25          -->26         <set name="students" cascade="all">27             <!-- 28                 key是用来描述外键29                  column就代表外键的名字30              -->31              <key>32                  <column name="Cid"></column>33              </key>34              <!-- 35                  classes和哪个对象进行关联36               -->37              <one-to-many class="cn.test.test.Student"/>38         </set>39         40     </class>41 </hibernate-mapping>

 

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5     <class name="cn.test.test.Student" table="student"> 6         <id name="Sid" type="java.lang.Long" length="5"> 7             <column name="Sid"></column> 8             <generator class="increment"></generator> 9         </id>10         <property name="Sname" type="java.lang.String" length="20"></property>11         <property name="Sdesc" type="java.lang.String" length="50"></property>12     </class>13 </hibernate-mapping>

 

 

技术分享
  1 package cn.test.test;  2   3 import java.util.HashSet;  4 import java.util.List;  5 import java.util.Set;  6   7 import javax.crypto.interfaces.PBEKey;  8   9 import org.hibernate.Session; 10 import org.hibernate.SessionFactory; 11 import org.hibernate.Transaction; 12 import org.hibernate.cfg.Configuration; 13 import org.junit.Test; 14  15 public class DoTest { 16     /** 17      * 1、保存班级 18      * 2、保存学生 19      * 3、保存班级的时候同时保存学生 20      * 4、保存班级的时候同时保存学生,并且建立班级和学生之间的关系 21      * 5、已经存在一个班级,新建一个学生,并且建立该学生和该班级之间的关系 22      * 6、已经存在一个学生,新建一个班级,并且建立该学生和该班级之间的关系 23      * 7、已经存在一个学生,已经存在一个班级,解除该学生和原来班级之间的关系,建立该学生和新班级之间的关系 24      * 8、已经存在一个学生,解除该学生和该学生所在班级之间的关系 25      * 9、解除该班级和所有的学生之间的关系,再重新建立该班级和一些新的学员之间的关系 26      * 10、解除该班级和所有的学生之间的关系 27      * 11、删除班级 28      *      * 29      *          *  解除该班级和所有的学生之间的关系 30      *          *  删除该班级 31      *      *    32      *          删除班级的同时删除学生 33      * 12、删除学生 34      *      同删除班级 35      */ 36      37     private static SessionFactory sessionFactory; 38     static{ 39         Configuration configuration=new Configuration(); 40         configuration.configure(); 41         sessionFactory=configuration.buildSessionFactory(); 42     } 43      44     //1、保存班级 45     @Test 46     public void testSaveClasses(){ 47         Session session=sessionFactory.openSession(); 48         Transaction transaction= session.beginTransaction(); 49         Classes classes=new Classes(); 50         classes.setCname("Tom"); 51         classes.setCdesc("1234567890-"); 52         session.save(classes); 53         transaction.commit(); 54         session.close(); 55     } 56     //2、保存学生 57     @Test 58     public void testSaveStudents(){ 59         Session session=sessionFactory.openSession(); 60         Transaction transaction= session.beginTransaction(); 61         Student student=new Student(); 62         student.setSname("屌丝班"); 63         student.setSdesc("都是纯逗比纯屌丝"); 64         session.save(student); 65         transaction.commit(); 66         session.close(); 67     } 68     //3、保存班级的时候同时保存学生 69     /* 70      * Hibernate: select max(Sid) from student 71         Hibernate: select max(Cid) from classes 72         Hibernate: insert into student (Sname, Sdesc, Sid) values (?, ?, ?) 73         Hibernate: insert into classes (Cname, Cdesc, Cid) values (?, ?, ?)  74      */ 75     @Test 76     public void testSaveClasses_Students(){ 77         Session session=sessionFactory.openSession(); 78         Transaction transaction= session.beginTransaction(); 79          80         Student student=new Student(); 81         student.setSname("屌丝班"); 82         student.setSdesc("都是纯逗比纯屌丝");         83         session.save(student); 84          85         Classes classes=new Classes(); 86         classes.setCname("Tom"); 87         classes.setCdesc("1234567890-"); 88         session.save(classes); 89          90         transaction.commit(); 91         session.close(); 92     } 93      94     //4、保存班级的时候同时保存学生,并且建立班级和学生之间的关系 95     /*classes.hbm.xml中设置;<set name="students" cascade="save-update"> 96      * Hibernate: select max(Cid) from classes 97         Hibernate: select max(Sid) from student 98         Hibernate: insert into classes (Cname, Cdesc, Cid) values (?, ?, ?) 99         Hibernate: insert into student (Sname, Sdesc, Sid) values (?, ?, ?)100         Hibernate: update student set Cid=? where Sid=?101         102         保存班级的时候同时保存学生,并且建立班级和学生之间的关系103      *         在classes.hbm.xml文件中104      *         <set name="students" cascade="save-update">105      *      inverse的值为默认或者false,才能让classes维护student的关系106         设置<set name="students" cascade="save-update" inverse="true">107         Hibernate: select max(Cid) from classes108         Hibernate: select max(Sid) from student109         Hibernate: insert into classes (Cname, Cdesc, Cid) values (?, ?, ?)110         Hibernate: insert into student (Sname, Sdesc, Sid) values (?, ?, ?)111         不建立关联112      */113     @Test114     public void testSaveClasses_R_Students(){115         Session session=sessionFactory.openSession();116         Transaction transaction= session.beginTransaction();117         118         Student student=new Student();119         student.setSname("Tom");120         student.setSdesc("1234567890-");    121         122         //session.save(student);必须删除掉123         124         Set<Student> students=new HashSet<Student>();125         students.add(student);126         127         Classes classes=new Classes();128         classes.setCname("屌丝班");129         classes.setCdesc("都是纯逗比纯屌丝");130         131         classes.setStudents(students);132         133         session.save(classes);134         135         transaction.commit();136         session.close();137     }138     139     140     //5、已经存在一个班级,新建一个学生,并且建立该学生和该班级之间的关系141     /**142      *  Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?143         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=?144         Hibernate: select max(Sid) from student145         Hibernate: insert into student (Sname, Sdesc, Sid) values (?, ?, ?)146         Hibernate: update student set Cid=? where Sid=?147      */148     @Test149     public void testSaveClassesAndStudents_R(){150         Session session=sessionFactory.openSession();151         Transaction transaction=session.beginTransaction();152         Classes classes=(Classes) session.get(Classes.class, 1L);153         Student student=new Student();154         student.setSname("Lucy");155         student.setSdesc("qwertyuiop");156         Set<Student> set=new HashSet<Student>();157         //建立关系158         classes.getStudents().add(student);159         160         //classes.setStudents(set);   把班级中的学生重新更新了161         162         transaction.commit();163         session.close();164     }165     //6、已经存在一个学生,新建一个班级,并且建立该学生和该班级之间的关系166     /*167      *  Hibernate: select student0_.Sid as Sid0_0_, student0_.Sname as Sname0_0_, student0_.Sdesc as Sdesc0_0_ from student student0_ where student0_.Sid=?168         Hibernate: select max(Cid) from classes169         Hibernate: insert into classes (Cname, Cdesc, Cid) values (?, ?, ?)170         Hibernate: update student set Cid=? where Sid=?171      */172     @Test173     public void testSaveStudentAndClasses_R(){174         Session session =sessionFactory.openSession();175         Transaction transaction= session.beginTransaction();176         177         Student student=(Student) session.get(Student.class, 2L);178         179         Classes classes=new Classes();180         classes.setCname("文艺青年班");181         classes.setCdesc("感觉都是文艺青年棒棒哒");182         183         Set<Student> set=new HashSet<Student>();184         set.add(student);185         classes.setStudents(set);186         187         session.save(classes);188         189         transaction.commit();190         session.close();191     }192     193     //7、已经存在一个学生,已经存在一个班级,解除该学生和原来班级之间的关系,建立该学生和新班级之间的关系194     /*195      *  Hibernate: select student0_.Sid as Sid0_0_, student0_.Sname as Sname0_0_, student0_.Sdesc as Sdesc0_0_ from student student0_ where student0_.Sid=?196         Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?197         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=?198         Hibernate: update student set Cid=? where Sid=?//建立关系199      * 200      */201     @Test202     public void testSaveRemove_Student_Classes(){203         Session session=sessionFactory.openSession();204         Transaction transaction=session.beginTransaction();205         Student student=(Student) session.get(Student.class, 2L);206         207         Classes classes=(Classes) session.get(Classes.class, 1L);208         classes.getStudents().add(student);//建立关系,没有必要删除与原来班级的关系,建立关系之后,自动解除与原来班级的关系209         transaction.commit();210         session.close();211     }212     //8、已经存在一个学生,解除该学生和该学生所在班级之间的关系213     /*214      *  Hibernate: select student0_.Sid as Sid0_0_, student0_.Sname as Sname0_0_, student0_.Sdesc as Sdesc0_0_ from student student0_ where student0_.Sid=?215         Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?216         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=?217         Hibernate: update student set Cid=null where Cid=? and Sid=?218      * 219      */220     @Test221     public void testRemoveStudent(){222         Session session=sessionFactory.openSession();223         Transaction transaction =session.beginTransaction();224         225         Student student=(Student) session.get(Student.class, 1L);226         Classes classes=(Classes)session.get(Classes.class, 1L);227         classes.getStudents().remove(student);228         transaction.commit();229         session.close();230     }231     232     //9、解除该班级和所有的学生之间的关系,再重新建立该班级和一些新的学员之间的关系233     /*234      *  Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?235         Hibernate: select student0_.Sid as Sid0_, student0_.Sname as Sname0_, student0_.Sdesc as Sdesc0_ from student student0_ where student0_.Sid in (3 , 4 , 5)236         Hibernate: update student set Cid=null where Cid=?237         Hibernate: update student set Cid=? where Sid=?238         Hibernate: update student set Cid=? where Sid=?239         Hibernate: update student set Cid=? where Sid=?240      * 241      */242     @Test243     public void testRemoveAllAndAddSome(){244         Session session =sessionFactory.openSession();245         Transaction transaction=  session.beginTransaction();246         247         Classes classes=(Classes) session.get(Classes.class, 1L);248         List<Student> students= session.createQuery("FROM Student WHERE Sid IN(3,4,5) ").list();249         250         Set<Student> sets=new HashSet<Student>(students);        251         classes.setStudents(sets);252         253         transaction.commit();254         session.close();255     }256     257     //10、解除该班级和所有的学生之间的关系258     /*259      *  Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?260         Hibernate: update student set Cid=null where Cid=?261      */262     @Test263     public void testRemoveAll(){264         Session session=sessionFactory.openSession();265         Transaction transaction =session.beginTransaction();266         267         Classes classes=(Classes) session.get(Classes.class, 1L);268         classes.setStudents(null);269         270         transaction.commit();271         session.close();272     }273     /*274      * 11、删除班级275      *      *276      *          *  解除该班级和所有的学生之间的关系277      *          *  删除该班级278      *      *   279      *          删除班级的同时删除学生280      * 12、删除学生281      *      同删除班级282      * 283      */284     //11、删除学生,同删除班级285     /*286      *  Hibernate: select classes0_.Cid as Cid1_0_, classes0_.Cname as Cname1_0_, classes0_.Cdesc as Cdesc1_0_ from classes classes0_ where classes0_.Cid=?287         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=?288         Hibernate: update student set Cid=null where Cid=?289         Hibernate: delete from student where Sid=?290         Hibernate: delete from student where Sid=?291         Hibernate: delete from student where Sid=?292         Hibernate: delete from classes where Cid=?293      */294     @Test295     public void testDeleteClasses(){296         Session session =sessionFactory.openSession();297         Transaction transaction=session.beginTransaction();298         299         Classes classes=(Classes) session.get(Classes.class, 1L);300         session.delete(classes);301         302         transaction.commit();303         session.close();304     }    305 }
一对多单向关联例子

 

Hibernate 一对多单向关联Demo