溫故知新:hibernate_08_表關聯_一對一雙向關聯

          一對一雙向關聯,在一對一的單向的基礎上,在不維護關系的那一端添加one-to-one配置
          實體類和配置如下
          package domain;

          public class Card {
              
              private int id;
              private String no;
              private Person person;
              //get/set和構造省略,但實際不可省略
              
          }

           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 <!-- Generated 2014-7-9 7:00:47 by Hibernate Tools 3.4.0.CR1 -->
           5 <hibernate-mapping>
           6     <class name="domain.Card" table="CARD">
           7         <id name="id" type="int">
           8             <column name="ID" />
           9             <generator class="native" />
          10         </id>
          11         <property name="no" type="java.lang.String">
          12             <column name="NO" />
          13         </property>
          14         <many-to-one column="pid" name="person" class="domain.Person" unique="true"/>
          15     </class>
          16 </hibernate-mapping>
          17 

          package domain;

          public class Person {
              
              private int id;
              private String name;
              private Card card;
              //get/set和構造省略,但實際不可省略
              
          }

           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 <!-- Generated 2014-7-9 7:00:47 by Hibernate Tools 3.4.0.CR1 -->
           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         <one-to-one name="card" class="domain.Card" property-ref="person"/>
          15     </class>
          16 </hibernate-mapping>
          17 
          書寫測試類
            1 package demo;
            2 
            3 import org.hibernate.Session;
            4 import org.junit.Test;
            5 
            6 import domain.Card;
            7 import domain.Person;
            8 import util.HibernateUtil;
            9 
           10 public class App 
           11 {
           12     @Test
           13     /**
           14      * 一對一雙向,關系由card的控制
           15      */
           16     public void test01(){
           17         
           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             Card card = new Card();
           28             card.setNo("#1");
           29             card.setPerson(person);
           30             
           31             session.save(card);
           32             
           33             session.getTransaction().commit();
           34             
           35         } catch (Exception e) {
           36             e.printStackTrace();
           37             if (session!=null) {
           38                 session.getTransaction().rollback();
           39             }
           40         }finally{
           41             if (session!=null) {
           42                 session.close();
           43             }
           44         }
           45         
           46     }
           47     
           48     @Test
           49     /**
           50      * 一對一雙向,關系由card的控制,重復添加多個card給一個person會報外鍵約束的異常
           51      */
           52     public void test02(){
           53         
           54         Session session = null;
           55         try {
           56             session = HibernateUtil.openSession();
           57             session.beginTransaction();
           58             
           59             Person person = (Person) session.load(Person.class, 1);
           60         
           61             Card card = new Card();
           62             card.setNo("#8");
           63             card.setPerson(person);
           64             
           65             session.save(card);
           66             
           67             session.getTransaction().commit();
           68             
           69         } catch (Exception e) {
           70             e.printStackTrace();
           71             if (session!=null) {
           72                 session.getTransaction().rollback();
           73             }
           74         }finally{
           75             if (session!=null) {
           76                 session.close();
           77             }
           78         }
           79         
           80     }
           81     
           82     @Test
           83     /**
           84      * 一對一雙向關聯,需要使用load方法取不維護關系的一端,會直接取得關聯的一端,不使用懶加載
           85      * 因為不維護關系,也就不存在外鍵,不存在外鍵也就不存在懶加載,一對一雙向關聯在實際開發中
           86      * 不建議使用,因為查詢其中一方,如果設置不當,會產生嵌套查詢
           87      */
           88     public void test03(){
           89         
           90         Session session = null;
           91         try {
           92             session = HibernateUtil.openSession();
           93             session.beginTransaction();
           94             
           95             Person person = (Person) session.load(Person.class, 1);
           96         
           97             System.out.println(person.getName());
           98             System.out.println(person.getCard().getNo());
           99             
          100             session.getTransaction().commit();
          101             
          102         } catch (Exception e) {
          103             e.printStackTrace();
          104             if (session!=null) {
          105                 session.getTransaction().rollback();
          106             }
          107         }finally{
          108             if (session!=null) {
          109                 session.close();
          110             }
          111         }
          112     }
          113     
          114     @Test
          115     /**
          116      * 一對一雙向關聯,使用load方法取維護關系的一端,不會直接取得關聯的一端,使用懶加載
          117      * 但是一旦取得不維護關系的一端,會發現不維護關系的一端關聯另一個實體,會直接查詢,這樣
          118      * 會導致冗余操作
          119      */
          120     public void test04(){
          121         
          122         Session session = null;
          123         try {
          124             session = HibernateUtil.openSession();
          125             session.beginTransaction();
          126             
          127             Card card = (Card) session.load(Card.class, 1);
          128             
          129             System.out.println(card.getNo());
          130             System.out.println(card.getPerson().getName());
          131             
          132             session.getTransaction().commit();
          133             
          134         } catch (Exception e) {
          135             e.printStackTrace();
          136             if (session!=null) {
          137                 session.getTransaction().rollback();
          138             }
          139         }finally{
          140             if (session!=null) {
          141                 session.close();
          142             }
          143         }
          144     }
          145     
          146 }
          147 

          posted on 2015-01-21 09:39 都較瘦 閱讀(96) 評論(0)  編輯  收藏 所屬分類: ORMFramework

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

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

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 突泉县| 如皋市| 南召县| 庆元县| 黄浦区| 横峰县| 澄城县| 高邑县| 黄梅县| 韩城市| 屏东县| 都安| 沂源县| 县级市| 隆昌县| 平武县| 沂水县| 平江县| 兴化市| 巴楚县| 左贡县| 通许县| 邯郸县| 天津市| 长沙市| 曲阳县| 奉节县| 梨树县| 丹凤县| 高淳县| 九龙坡区| 紫云| 无棣县| 会理县| 宝坻区| 滁州市| 龙南县| 新干县| 福州市| 泰宁县| 枞阳县|