hibernate学习笔记第10讲-一对多关联映射one-to-many双向

 

hihernate一对多关联映射(双向Classes<----->Student

 

一对多双向关联映射:

         * 在一一端的集合上使用<key>,在对方表中加入一个外键指向一一端

         * 在多一端采用<many-to-one>

        

注意:<key>标签指定的外键字段必须和<many-to-one>指定的外键字段一致,否则引用字段的错误

        

如果在”一“一端维护一对多关联关系,hibernate会发出多余的udpate语句,所以我们一般在多

的一端来维护关联关系

 

关于inverse属性:

         inverse主要用在一对多和多对多双向关联上,inverse可以被设置到集合标签<set>上,

         默认inversefalse,所以我们可以从”一“一端和”多“一端维护关联关系,

         如果设置成inversetrue,则我们只能从多一端来维护关联关系

        

         注意:inverse属性,只影响数据的存储,也就是持久化

                  

inversecascade

         * inverse是关联关系的控制方向

         * cascade操作上的连锁反应


hibernate学习笔记第10讲-一对多关联映射one-to-many双向
 映射关系:

Student.hbm.xml

<hibernate-mapping>

         <class name="com.bjsxt.hibernate.Student" table="t_student">

                   <id name="id">

                            <generator class="native"/>

                   </id>

                   <property name="name"/>

                   <many-to-one name="classes" column="classesid"/>

         </class>

</hibernate-mapping>

 

Classes.hbm.xml

<hibernate-mapping package="com.bjsxt.hibernate">

         <class name="Classes" table="t_classes">

                   <id name="id">

                            <generator class="native"/>

                   </id>

                   <property name="name"/>

//默认inverse = false;反转,true时表示一的一端维护关系失效。反转到多的一端维护关系。不会发出updae语句。

                   <set name="students" inverse="true" cascade="all">

                            <key column="classesid"/>

                            <one-to-many class="Student"/>

                   </set>

         </class>

</hibernate-mapping>

Inverse = truecascade = all 保存:

 

session.beginTransaction();

                            Classes classes = new Classes();

                            classes.setName("尚学堂");

                           

                            Student student1 = new Student();

                            student1.setName("10");

                            student1.setClasses(classes);

                           

                            Student student2 = new Student();

                            student2.setName("祖儿");

                            student2.setClasses(classes);

                           

                            Set students = new HashSet();

                            students.add(student1);

                            students.add(student2);

                           

                            classes.setStudents(students);

                           

                            //保存时会反转到student一端,由于cascade = all,会先把class保存,再保存student

                            session.save(classes);

                           

                            session.getTransaction().commit();

 

常用保存方法:在多的一端维护关系

session.beginTransaction();

                           

                            Classes classes = new Classes();

                            classes.setName("尚学堂");

                            session.save(classes);

                           

                            Student student1 = new Student();

                            student1.setName("10");

                            student1.setClasses(classes);

                            session.save(student1);

                           

                            Student student2 = new Student();

                            student2.setName("祖儿");

                            student2.setClasses(classes);

                            session.save(student2);

                           

                            session.getTransaction().commit();

你可能感兴趣的:(java,Hibernate,jdbc,领域模型)