一对多与多对一区别:
映射原理是一致的,都是在多的一端加入一个外键,指向一的一端
区别在于维护的关系不同:
多对一维护的关系是:多指向一的,有了此关系,在加载多的时候可以将一加载上来
一对多维护的关系是:一指向多的关系,有了此关系,加载一
的时候可以将多加载上来
单向
适用情况:一个班级有多个学生(班级依赖学生,必须先有学生,班级维护关系)
关系图:
注:都是在多的一端加上外键,指向一的一端
<span style="font-size:18px;">实体: Student: public class Student { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } Class: public class Classes { private int id; private String name; private Set students; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getStudents() { return students; } public void setStudents(Set students) { this.students = students; } } 映射文件: Student: <hibernate-mapping> <class name="com.bjpowernode.hibernate.Student" table="t_student"> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping> Class: <hibernate-mapping> <class name="com.bjpowernode.hibernate.Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="students"> <key column="classesid"/> <one-to-many class="com.bjpowernode.hibernate.Student"/> </set> </class> </hibernate-mapping> 客户端: public void testSave2() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Student student1 = new Student(); student1.setName("张三"); session.save(student1); Student student2 = new Student(); student2.setName("李四"); session.save(student2); Classes classes = new Classes(); classes.setName("动力节点"); Set students = new HashSet(); students.add(student1); students.add(student2); classes.setStudents(students); // 可以成功保存数据 // 但是会发出多余的update语句来维持关系 session.save(classes); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } }</span>
结果:
双向
适用情况:
两端都知道关系,解决一对多单向关联的缺陷
关系图:
<span style="font-size:18px;">实体: student: public class Student { private int id; private String name; private Classes classes; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } } class: public class Classes { private int id; private String name; private Set students; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getStudents() { return students; } public void setStudents(Set students) { this.students = students; } } 映射文件: student: <hibernate-mapping> <class name="com.bjpowernode.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></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;">class:</span></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"> <hibernate-mapping></span></span>
<span style="font-size:18px;"> <class name="com.bjpowernode.hibernate.Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="students" inverse="true"> <key column="classesid"/> <one-to-many class="com.bjpowernode.hibernate.Student"/> </set> </class> </hibernate-mapping></span>
客户端:
<span style="font-size:18px;"> 情况1: public void testSave2() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Student student1 = new Student(); student1.setName("张三"); session.save(student1); Student student2 = new Student(); student2.setName("李四"); session.save(student2); Classes classes = new Classes(); classes.setName("动力节点"); Set students = new HashSet(); students.add(student1); students.add(student2); classes.setStudents(students); //可以成功保存数据 //但是会发出多余的update语句来维持关系 session.save(classes); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); finally { HibernateUtils.closeSession(session); } }</span>
注:可以保存数据,但会发出多余的update语句来维护关系
情况2:
<span style="font-size:18px;">public void testSave3() { Session session = null; try { <span style="white-space:pre"> </span>session = HibernateUtils.getSession(); session.beginTransaction(); Classes classes = new Classes(); classes.setName("动力节点"); session.save(classes); Student student1 = new Student(); student1.setName("张三"); student1.setClasses(classes); session.save(student1); Student student2 = new Student(); student2.setName("李四"); student2.setClasses(classes); session.save(student2); session.getTransaction().commit(); catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } }</span>
结果:
时间: 2024-10-26 01:24:03