星空网 > 软件开发 > 数据库

Hibernate关联映射(单项多对一和一对多、双向一对多)

  最近总是接触着新的知识点来扩展自己的知识面;不停的让自己在原地接触天空的感觉真的很美好!!!革命没有成功,程序员的我们怎么能不努力呢......

 一、用员工和部门来剖析关联映射的原理。

Hibernate关联映射(单项多对一和一对多、双向一对多)

1)从这张截图可以看出我会站在员工的角度讲述:(单向关联)

  关系:(多个员工对应一个部门)多对一的关系

  意味:多个Emp对象只会引用一个Dept对象

  方法:在Emp类中定义一个Dept类型属性,来引用所有关联的Dept对象

eg.

  第一步建立两个实体类省略

  第二步建立大配置

  

<?'1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>        <session-factory>    <!-- Database connection settings 数据库连接设置-->    <!-- 驱动类 -->    <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>    <!-- url地址 -->    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>    <property name="connection.username">sa</property>    <property name="connection.password">1</property>    <!-- SQL dialect (SQL 方言) -->    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>    <!-- Echo all executed SQL to stdout 在控制台打印后台的SQL语句 -->    <property name="show_sql">true</property>            <!-- 格式化显示SQL -->    <property name="format_sql">true</property>          <!-- Drop and re-create the database schema on startup -->     <property name="hbm2ddl.auto">update</property>           <!-- <property name="current_session_context_class">thread</property> -->     <!-- 配置Hibernate.cfg."hibernate.cache.use_second_level_cache">true</property>     <!-- 配置二级缓存的供应商 -->    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>     <!-- * 在hibernate.cfg."hibernate.cache.use_query_cache">true</property>                   <!-- 关联小配置 -->     <mapping resource="cn/happy/entity/Dept.hbm." />    <mapping resource="cn/happy/entity/Emp.hbm." />          <class-cache  usage="read-write" class="cn.happy.entity.Dept"/>  </session-factory></hibernate-configuration>

  第三步创建小配置Emp

 <!-- 植入一个Dept对象 : 多对一-->   <many-to-one name="dept" column="deptNo"></many-to-one> 关键代码
   解析:<many-to-one>:元素建立了Dept属性和Emp表的DEPTNo之间的映射关联
      name:设定持久化类的属性名
      column:name对应表的外键(Emp表的外键)
      class:设定持久化类的属性类型
<?"1.0"?><!DOCTYPE hibernate-mapping PUBLIC   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.happy.entity">  <class name="Emp" table="EMP">   <id name="empId">    <generator class="sequence">     <param name="sequence">SEQ_NUM</param>    </generator>   </id>    <property name="empName" type="string"/>      <!-- 植入一个Dept对象 : 多对一-->   <many-to-one name="dept" class="Dept" column="deptNo"></many-to-one>  </class></hibernate-mapping>

第四步创建小配置Dept

<?"1.0"?><!DOCTYPE hibernate-mapping PUBLIC   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.happy.entity">  <class name="Dept" table="DEPT">  <!-- <cache usage="read-write"/> -->   <id name="deptNo">    <generator class="sequence">     <param name="sequence">SEQ_NUM</param>    </generator>   </id>    <property name="deptName"/>  </class></hibernate-mapping>

第五步就是测试类

@Test    public void AddObjectToDept(){      Emp emps=new Emp();      emps.setEmpName("西丽");    Session session=HibernateUtil.currentSession();      Transaction tx = session.beginTransaction();    session.save(emps);    System.out.println("ok");    tx.commit();    HibernateUtil.closeSession();    }

第六步运行结果

Hibernate关联映射(单项多对一和一对多、双向一对多)

上述结果:现将数据查找一遍确保没有在进行添加

Hibernate关联映射(单项多对一和一对多、双向一对多)

 2)从截图可以看出我接下来就要站在部门Dept的角度剖析:(单项关联)

   关系:(部门)一对多关联

   意味:每一个Dept对象会引用一组Emp对象

   方法:Dept类中应该定义一个集合类型属性,来引用所有关联的Emp对象。

  与上述步骤唯一不同的就是Dept.hbm.

  关键代码一对多<set name="emps">

            <key column="deptNo"></key>             <one-to-many />         </set>

 

<?"1.0"?><!DOCTYPE hibernate-mapping PUBLIC   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.happy.entity">  <class name="Dept" table="DEPT">  <!-- <cache usage="read-write"/> -->   <id name="deptNo">    <generator class="sequence">     <param name="sequence">SEQ_NUM</param>    </generator>   </id>    <property name="deptName"/>   <set name="emps">    <key column="deptNo"></key>    <one-to-many class="Emp" />   </set>  </class></hibernate-mapping>

二、双向关联

  Hibernate关联映射(单项多对一和一对多、双向一对多)

如上图所示:满足多对一和一对多的关联就是双向关联。

三、cascade属性

  问题:删除Dept对象,并级联删除与Dept对象关联的Emp对象

  解析:我们可以在<set>、<many-to-one>元素里面写cascade=“delete”属性就能完成并级联删除

   

cascade属性值              描述                    
none      当Session操纵当前对象,忽略其它的关联对象   cascade的默认值none
save-update      保存或者更新数据
delete    当通过Session的delete()方法删除当前对象时会级联删除所有的关联的对象
all    包含save—update和delete

      初学者的我理解所致,既有不足,请多多指教。




原标题:Hibernate关联映射(单项多对一和一对多、双向一对多)

关键词:Hibernate

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流