Rhythmk 学习 Hibernate 05 - Hibernate 表间关系 [One To One]
            时间:2014-05-05 23:06:28  
            收藏:0  
            阅读:516
        
        
        1、One To One 单相
背景:
古代一个老婆 只能关联一个老公
husband.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | packagecom.rhythmk.model;publicclasshusband {        publicInteger getHusbandId() {        returnhusbandId;    }    publicvoidsetHusbandId(Integer husbandId) {        this.husbandId = husbandId;    }    publicString getName() {        returnName;    }    publicvoidsetName(String name) {        Name = name;    }    privateInteger husbandId;    privateString Name;} | 
hasband.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rhythmk.model"> <class name="husband" table="t_husband"> <id name="husbandId" type="int"> <column name="husbandId" /> <generator class="native" /> </id> <property name="Name" type="string"> </property> </class> </hibernate-mapping>
wife.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | packagecom.rhythmk.model;publicclasswife {    privateInteger wifeId;    privateString name;    publicInteger getWifeId() {        returnwifeId;    }    publicvoidsetWifeId(Integer wifeId) {        this.wifeId = wifeId;    }    publicString getName() {        returnname;    }    publicvoidsetName(String name) {        this.name = name;    }        publichusband getHb() {        returnhb;    }    publicvoidsetHb(husband hb) {        this.hb = hb;    }    privatehusband  hb;    @Override    publicString toString() {        return"wife [wifeId="+ wifeId + ", name="+ name + ", hb="+ hb + "]";    }    } | 
wife.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rhythmk.model"> <class name="wife" table="t_wife"> <id name="wifeId" type="int"> <column name="wifeId" /> <generator class="native" /> </id> <property name="Name" type="string"> </property> <!-- one to one 类似于 one to many 只需要添加 unique =true 即可 --> <many-to-one name="hb" column="husbandId" ></many-to-one> </class> </hibernate-mapping>
注意:
one to one 类似于 one to many 只需要添加 unique =true 即可
2、One To One 双向
跟单相查不多 就是两边都配上 many to one
调整 hasband.hbm.xml 添加:
<!-- one to one 类似于 one to many 只需要添加 unique =true 即可 --> <many-to-one name="mywife" column="wifeId" ></many-to-one>
调整 husband.java :
| 1 2 3 4 5 6 7 | publicwife getMywife() {    returnmywife;}publicvoidsetMywife(wife mywife) {    this.mywife = mywife;}privatewife mywife; | 
测试:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | @Testpublicvoidtest02_add() {    Session session = null;    try{        session = HibernateUtil.getSessionFactory().openSession();        session.beginTransaction();        husband hb = newhusband();        hb.setName("王大1");        wife w = newwife();        w.setName("张妞 2");        w.setHb(hb);        hb.setMywife(w);        session.save(hb);        session.save(w);        System.out.println(w.toString());        session.getTransaction().commit();    } catch(Exception e) {        e.printStackTrace();    } finally{        if(session != null)            session.close();    }} | 
输出:
Hibernate: insert into t_husband (Name, wifeId) values (?, ?)
Hibernate: 
insert into t_wife (Name, husbandId) values (?, ?)
wife [wifeId=6, name=张妞 2, 
hb=com.rhythmk.model.husband@8b677f]
Hibernate: update t_husband set Name=?, 
wifeId=? where husbandId=?
Rhythmk 学习 Hibernate 05 - Hibernate 表间关系 [One To One],布布扣,bubuko.com
            评论(0)
        
        
         
        