Rhythmk 学习 Hibernate 06 - Hibernate 表间关系 [One To One]
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 | package com.rhythmk.model; public class husband { public Integer getHusbandId() { return husbandId; } public void setHusbandId(Integer husbandId) { this .husbandId = husbandId; } public String getName() { return Name; } public void setName(String name) { Name = name; } private Integer husbandId; private String 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 | package com.rhythmk.model; public class wife { private Integer wifeId; private String name; public Integer getWifeId() { return wifeId; } public void setWifeId(Integer wifeId) { this .wifeId = wifeId; } public String getName() { return name; } public void setName(String name) { this .name = name; } public husband getHb() { return hb; } public void setHb(husband hb) { this .hb = hb; } private husband hb; @Override public String 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 | public wife getMywife() { return mywife; } public void setMywife(wife mywife) { this .mywife = mywife; } private wife 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 | @Test public void test02_add() { Session session = null ; try { session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); husband hb = new husband(); hb.setName( "王大1" ); wife w = new wife(); 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=?
代码下载地址:
http://pan.baidu.com/s/1gdFt7ur
一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥