21_22_一对多关联映射_单向_1、2 v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0cm; margin-bottom:.0001pt; text-align:center; mso-pagination:none; tab-stops:center 207.65pt right 415.3pt; layout-grid-mode:char; border:none; mso-border-bottom-alt:solid windowtext .75pt; padding:0cm; mso-padding-alt:0cm 0cm 1.0pt 0cm; font-size:9.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} p.MsoFooter, li.MsoFooter, div.MsoFooter {margin:0cm; margin-bottom:.0001pt; mso-pagination:none; tab-stops:center 207.65pt right 415.3pt; layout-grid-mode:char; font-size:9.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no; mso-footnote-separator:url("21_22_一对多关联映射_单向_1、2.files/header.htm") fs; mso-footnote-continuation-separator:url("21_22_一对多关联映射_单向_1、2.files/header.htm") fcs; mso-endnote-separator:url("21_22_一对多关联映射_单向_1、2.files/header.htm") es; mso-endnote-continuation-separator:url("21_22_一对多关联映射_单向_1、2.files/header.htm") ecs;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->
21_22_ 一对多关联映射 _ 单向 _1 、 2
例子:
Student.java 、 Classes.java 、 Student.hbm.xml 、 Classes.hbm.xml 、 ExportDB.java 、 HibernateUtils.java 、 one2many.java |
hihernate 一对多关联映射(单向 Classes----->Student )
t_classes id | name 1 | 尚学堂 |
t_student id | name | classesid 1 | 10 | 1 2 | 祖儿 | 1 |
一对多关联映射利用了多对一关联映射原理
多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向一
一对多关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是一指向多
也就是说一对多和多对一的映射策略是一样的,只是站的角度不同
在一一端维护关系的缺点:
* 如果将 t_student 表里的 classesid 字段设置为非空,则无法保存
* 因为不是在 student 这一端维护关系,所以 student 不知道是哪个班的,
所以需要发出多余的 update 语句来更新关系
一对多和多对一关系,这两个没多少区别,都是在 多 的这边加上外键指向 一 的这边,
主要的区别是:
如果维护的是多对一,当加载多 ( 多个学生 ) 的时候,它能把一 ( 多个学生对应的那个一个班级 ) 加载进来 ;
如果维护的是一对多,当加载一 ( 一个班级 ) 的时候,它能把多 ( 一个班级里含有的多个学生 ) 加载进来 ;
例子: Student.java 代码
package com.bjsxt.hibernate;
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; } } |
Classes.java 代码
package com.bjsxt.hibernate;
import java.util.Set;
public class Classes { private int id ; private String name ; private Set studentes ;
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 getStudentes() { return studentes ; } public void setStudentes(Set studentes) { this . studentes = studentes; } } |
Student.hbm.xml 代码
<? xml version = "1.0" ?> <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > < hibernate-mapping > < class name = "com.bjsxt.hibernate.Student" table = "t_Student" > < id name = "id" > < generator class = "native" /> </ id > < property name = "name" /> </ class > </ hibernate-mapping > |
Classes.hbm.xml 代码
<? xml version = "1.0" ?> <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > < hibernate-mapping > < class name = "com.bjsxt.hibernate.Classes" table = "t_Classes" > < id name = "id" > < generator class = "native" /> </ id > < property name = "name" />
<!-- 在 Student 实体里没有 classesid 字段,所以在这里加上个外键 classesid, 作为外键指向 Student , --> < set name = "studentes" > < key column = "classesid" /> < one-to-many class = "com.bjsxt.hibernate.Student" /> </ set > </ class > </ hibernate-mapping > |
HibernateUtils.java 代码
package com.bjsxt.hibernate;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory ;
static { try { Configuration cfg = new Configuration().configure(); factory = cfg.buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); } }
public static SessionFactory getSessionFactory() { |