溫故知新:hibernate_07_表關聯_一對一單向關聯

          一對一和多對一在配置上基本一致,只是需要在配置關聯的一方添加unique="true"(基于XML配置而言),以人和身份證為例。
          Idcard
           1 package domain;
           2 
           3 public class Idcard {
           4     
           5     private int id;
           6     private String num;
           7     private Person person;
           8     //get/set和構造省略,但實際不可省略
           9     
          10 }
          11 

           1 <?xml version="1.0"?>
           2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
           3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
           4 
           5 <hibernate-mapping>
           6     <class name="domain.Idcard" table="IDCARD">
           7         <id name="id" type="int">
           8             <column name="ID" />
           9             <generator class="native" />
          10         </id>
          11         <property name="num" type="java.lang.String">
          12             <column name="NUM" />
          13         </property>
          14         <!-- 注意:unique配置只有在hibernate生成生成DDL的時候才會啟用,若已存在數據表,則不會檢查唯一性約束 -->
          15         <!-- 配置unique="true",指明該多對一多的一方只有一個 -->
          16         <many-to-one name="person" column="pid" class="domain.Person" unique="true" />
          17     </class>
          18 </hibernate-mapping>
          19 

           1 package domain;
           2 
           3 public class Person {
           4     
           5     private int id;
           6     private String name;
           7     //get/set和構造省略,但實際不可省略
           8     
           9 }
          10 

           1 <?xml version="1.0"?>
           2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
           3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
           4 
           5 <hibernate-mapping>
           6     <class name="domain.Person" table="PERSON">
           7         <id name="id" type="int">
           8             <column name="ID" />
           9             <generator class="native" />
          10         </id>
          11         <property name="name" type="java.lang.String">
          12             <column name="NAME" />
          13         </property>
          14     </class>
          15     
          16 </hibernate-mapping>
          17 

          書寫測試類
           1 package demo;
           2 
           3 import org.hibernate.Session;
           4 import org.junit.Test;
           5 
           6 import domain.Idcard;
           7 import domain.Person;
           8 import util.HibernateUtil;
           9 
          10 public class App 
          11 {
          12     
          13     /**
          14      * 一對一單向關聯
          15      */
          16     @Test
          17     public void addTest01() {
          18         Session session = null;
          19         try {
          20             session = HibernateUtil.openSession();
          21             session.beginTransaction();
          22             
          23             Person person = new Person();
          24             person.setName("Nick");
          25             session.save(person);
          26             
          27             Idcard idcard = new Idcard();
          28             idcard.setNum("#2");
          29             idcard.setPerson(person);
          30             session.save(idcard);
          31             
          32             session.getTransaction().commit();
          33             
          34         } catch (Exception e) {
          35             if (session != null) {
          36                 session.getTransaction().rollback();
          37             }
          38         } finally{
          39             if (session != null) {
          40                 session.close();
          41             }
          42         }
          43     }
          44     
          45     /**
          46      * 一對一單向關聯,idcard一方維護一對一的關系,所以一個人不能有兩個idcard
          47      * 此處有異常
          48      */
          49     @Test
          50     public void addTest02() {
          51         Session session = null;
          52         try {
          53             session = HibernateUtil.openSession();
          54             session.beginTransaction();
          55             
          56             Person person = (Person) session.load(Person.class, 1);
          57             
          58             Idcard idcard = new Idcard();
          59             idcard.setNum("#2");
          60             idcard.setPerson(person);
          61             session.save(idcard);
          62             
          63             session.getTransaction().commit();
          64             
          65         } catch (Exception e) {
          66             e.printStackTrace();
          67             if (session != null) {
          68                 session.getTransaction().rollback();
          69             }
          70         } finally{
          71             if (session != null) {
          72                 session.close();
          73             }
          74         }
          75     }
          76 }
          77 

          posted on 2015-01-20 19:54 都較瘦 閱讀(97) 評論(0)  編輯  收藏 所屬分類: ORMFramework

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          公告

          博客定位:囿于目前的水平,博客定位在記錄自己的學習心得和隨手的練習

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 吐鲁番市| 诏安县| 抚松县| 六枝特区| 威宁| 兰溪市| 荣昌县| 道真| 武宣县| 襄城县| 齐河县| 新晃| 河津市| 灌南县| 丹江口市| 泗洪县| 肥城市| 土默特左旗| 临汾市| 甘谷县| 武宣县| 余姚市| 垣曲县| 山阳县| 延津县| 克拉玛依市| 滦南县| 离岛区| 溧阳市| 高雄县| 龙泉市| 昌宁县| 屯留县| 新乡县| 宜宾市| 宣化县| 肥乡县| 仙游县| 马鞍山市| 邵阳县| 孝感市|