hibernate的基礎學習--一對一關聯

來源:互聯網
上載者:User

標籤:

一對一關聯性以丈夫和妻子模型

設定檔

妻子設定檔:

<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- 映射配置 --><hibernate-mapping>    <class name="hib.po.Wife" table="t_wife">        <id name="wifeId" column="wife_id">            <generator class="uuid"></generator>        </id>        <property name="wifeName" column="wifename"></property>    </class></hibernate-mapping>
View Code

 

丈夫設定檔

 1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-mapping PUBLIC  3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <!-- 映射配置 --> 6 <hibernate-mapping> 7     <class name="hib.po.Husband" table="t_husband"> 8         <id name="husId" column="hus_id"> 9             <generator class="uuid"></generator>10         </id>11         12         <!-- 普通屬性和普通欄位對應 -->13         <property name="husName" column="husname"></property>14         <!-- 關聯關係:和Wife的one2one15             特殊的many2one16          -->17         <many-to-one 18             name="wife"19             class="hib.po.Wife"20             column="wife_id"21             unique="true"22          ></many-to-one>23     </class>24 </hibernate-mapping>
View Code

 

測試代碼:

 1 /** 2  * @author nunu 3  * 測試hibernate OneToMany  4  */ 5 public class TestHibernateOneToOne { 6  7     /** 8      * 添加資料 9      */10     @Test11     public void addUser() {12         SessionFactory sf = H3Util.getSessionFactory();13         Session session = sf.openSession();14         Transaction ts = session.beginTransaction();15         16         Husband hus1 = new Husband(null, "小明", null);17         Husband hus2 = new Husband(null, "小強", null);18         Wife wife1 = new Wife(null, "小花");19         Wife wife2 = new Wife(null, "小麗");20         21         session.save(hus1);22         session.save(hus2);23         session.save(wife1);24         session.save(wife2);25         System.out.println("成功");26         ts.commit();27         session.close();28     }29     30     @Test31     public void updateHusband() {32         SessionFactory sf = H3Util.getSessionFactory();33         Session session = sf.openSession();34         Transaction ts = session.beginTransaction();35         36         Husband hus1 = (Husband) session.get(Husband.class, "4028810a57d16fe00157d16fe3450000");37         Husband hus2 = (Husband) session.get(Husband.class, "4028810a57d16fe00157d16fe3590001");38         39         Wife wife1 = (Wife) session.get(Wife.class, "4028810a57d16fe00157d16fe3590002");40         Wife wife2 = (Wife) session.get(Wife.class, "4028810a57d16fe00157d16fe3590003");41 42         //維護43         hus1.setWife(wife1);44         hus2.setWife(wife2);45 46         //update:使用一級緩衝中的快照,如果檢測沒有發生改變,不會發送任何修改語句47         session.update(hus1);48         session.update(hus2);49         50         System.out.println(hus1);51         System.out.println(hus2);52         53         ts.commit();54         session.close();55     }56 }

 

hibernate的基礎學習--一對一關聯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.