单向的一对多关联关系(One to Many)
对象模型关系分析:
少的一端:会通过引用一个多端的集合对象,建立对象模型关系
多的一端:没有任何变化,不知道和少的一端存在着关系。
关系模型分析:
少的一端:没有任何变化
多的一端:会和少的一端建立外键关系
使用时需要少的一端所对应的XXX.hbm.xml中进行配置
<bag name="对多的一端对象集合的属性名称"> <!-- 用key元素指定关联的外键列,会在多的一端所对应的表中产生 --><key column="account_id" />
<!-- 用one-to-many元素关联到多端的实体类 -->
<one-to-many class="多的一端类的全限定名称" />
</bag>注意:
注意:
在有映射关系的实体类中,对于普通属性会进行数据的立即加载,而对于非普通属性默认采用的是延迟加载。
可以在XXX.hbm.xml关联文件中通过指定lazy="false" 采取立即加载方式。
对延迟加载的对象也可以使用Hibernate.initialize(Object proxy);方法进行强制初始化。
1package model; 2 3/** 4 * @author sally 5 * 单向的多对一 6 * 少的一端 7 */ 8public class Dept { 9 10 private int id; 11 private String deptName; 12 13 public Dept(){ 14 15 } 16 17 public int getId() { 18 return id; 19 } 20 21 public void setId(int id) { 22 this.id = id; 23 } 24 25 public String getDeptName() { 26 return deptName; 27 } 28 29 public void setDeptName(String deptName) { 30 this.deptName = deptName; 31 } 32 33} 34 35package model; 36 37import java.util.Date; 38 39/** 40 * 41 * @author sally 42 * Many to One 43 * 多的一端 44 */ 45public class Employee { 46 47 private int id; 48 private String empName; 49 private Date hiredate; 50 51 //对一端的引用 52 private Dept dept; 53 54 public Employee(){ 55 56 } 57 58 public int getId() { 59 return id; 60 } 61 62 public void setId(int id) { 63 this.id = id; 64 } 65 66 public String getEmpName() { 67 return empName; 68 } 69 70 public void setEmpName(String empName) { 71 this.empName = empName; 72 } 73 74 public Date getHiredate() { 75 return hiredate; 76 } 77 78 public void setHiredate(Date hiredate) { 79 this.hiredate = hiredate; 80 } 81 82 public Dept getDept() { 83 return dept; 84 } 85 86 public void setDept(Dept dept) { 87 this.dept = dept; 88 } 89 90} 91 92<?xml version="1.0"?> 93<!DOCTYPE hibernate-mapping PUBLIC 94 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 95 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 96<hibernate-mapping> 97 <!-- name属性指定类名(全限定名) table指明表名,不指明table数据默认的表名和实体名一致 --> 98 <class name="model.Dept" table="dept_tab"> 99 <!-- type指明当前字段的类型 name对应实体中的属性名 --> 100 <id type="integer" name="id"> 101 <!-- 提供ID自增的策略 native会根据数据库自行判断 --> 102 <generator class="native"/> 103 </id> 104 <property name="deptName" type="string"></property> 105 </class> 106</hibernate-mapping> 107 108<?xml version="1.0"?> 109<!DOCTYPE hibernate-mapping PUBLIC 110 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 111 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 112<hibernate-mapping> 113 <!-- name属性指定类名(全限定名) table指明表名,不指明table数据默认的表名和实体名一致 --> 114 <class name="model.Employee" table="emp_tab"> 115 <!-- type指明当前字段的类型 name对应实体中的属性名 --> 116 <id type="integer" name="id"> 117 <!-- 提供ID自增的策略 native会根据数据库自行判断 --> 118 <generator class="native"/> 119 </id> 120 <property name="empName" type="string"></property> 121 <property name="hiredate" type="timestamp"></property> 122 <!-- 用 many-to-one 元素映射多对一关联 123 name属性:指定关联的属性名 124 column属性:指定此关联属性在数据库表中的外键字段名 --> 125 <many-to-one name="dept" column="dept_id"></many-to-one> 126 127 </class> 128</hibernate-mapping> 129 130<!DOCTYPE hibernate-configuration PUBLIC 131 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 132 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 133 134<hibernate-configuration> 135 <session-factory> 136 <!-- 配置连接数据库的参数 --> 137 <!-- 配置数据库的方言 --> 138 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 139 <!-- 数据库驱动 --> 140 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 141 <property name="hibernate.connection.url">jdbc:mysql:///m2o</property> 142 <property name="hibernate.connection.username">root</property> 143 <property name="hibernate.connection.password">root</property> 144 <property name="hibernate.show_sql">true</property> 145 <!-- 其它属性配置 --> 146 <!-- 指明C3P0的提供者 --> 147 <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 148 <!-- 连接池参数的配置 --> 149 <property name="hibernate.c3p0.min_size">5</property> 150 <property name="hibernate.c3p0.max_size">30</property> 151 <property name="hibernate.c3p0.timeout">1800</property> 152 <property name="hibernate.c3p0.max_statements">50</property> 153 154 <!-- 打印SQL语句到控制台 --> 155 <property name="hibernate.show_sql">true</property> 156 <property name="hibernate.format_sql">true</property> 157 <property name="hibernate.hbm2ddl.auto">update</property> 158 <!-- 注册实体的对象关系映射文件 --> 159 160 <mapping resource="model/Dept.hbm.xml"/> 161 <mapping resource="model/Employee.hbm.xml"/> 162 </session-factory> 163</hibernate-configuration> 164 165package test; 166 167import java.util.Date; 168 169import model.Dept; 170import model.Employee; 171 172import org.hibernate.Hibernate; 173import org.hibernate.Session; 174import org.hibernate.Transaction; 175import org.hibernate.cfg.Configuration; 176import org.hibernate.tool.hbm2ddl.SchemaExport; 177import org.junit.Test; 178 179import util.HibernateUtils; 180 181public class EmployeeTest { 182 183 @Test 184 public void createTable(){ 185 Configuration cfg=new Configuration().configure(); 186 SchemaExport se=new SchemaExport(cfg); 187 se.create(true, true); 188 } 189 190 @Test 191 public void save(){ 192 Session session=HibernateUtils.getSession(); 193 Transaction ts=session.beginTransaction(); 194 195 //在单向的多对一中,要先添加少的一端 196 Dept dept=new Dept(); 197 dept.setDeptName("财务部"); 198 session.save(dept); 199 200 Employee emp=new Employee(); 201 emp.setEmpName("zhangsan"); 202 emp.setHiredate(new Date()); 203 emp.setDept(dept); 204 session.save(emp); 205 206 ts.commit(); 207 HibernateUtils.close(session); 208 } 209 210 @Test 211 public void get(){ 212 Session session=HibernateUtils.getSession(); 213 Transaction ts=session.beginTransaction(); 214 215 Employee emp=(Employee)session.get(Employee.class, 1); 216 System.out.println(emp.getEmpName()+"---->"+emp.getHiredate()); 217 Hibernate.initialize(emp.getDept()); 218 //Dept dept=emp.getDept(); 219 //System.out.println(dept.getId()+"-->"+dept.getDeptName()); 220 ts.commit(); 221 HibernateUtils.close(session); 222 } 223 224} 225 226package util; 227 228import org.hibernate.Session; 229import org.hibernate.SessionFactory; 230import org.hibernate.cfg.Configuration; 231 232public class HibernateUtils { 233 234 private static SessionFactory factory; 235 static{ 236 factory=new Configuration().configure().buildSessionFactory(); 237 } 238 public static SessionFactory getFactory(){ 239 return factory; 240 } 241 242 public static Session getSession(){ 243 return factory.openSession(); 244 } 245 246 public static void close(Session session){ 247 if(session!=null){ 248 session.close(); 249 } 250 251 } 252 253}