(十)Hibernate的一对一关联关系
一、概述
我们以Company与Address类为例,介绍映射一对一关联关系的方法。
一对一的关联方法有两种
-按照外键映射:两个表任意一个表定义一个外键,来关联另一个表。
-按照主键映射:一个表的主键同时作为外键,和另一个表的主键保持一致。
二、按照外键映射
(1)创建实体类
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 40 41 42 43 44 45 46 47 48 49 | public class Company { private Integer id; private String name; private Address address; public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this .address = address; } } public class Address { private Integer id; private String name; private Company company; public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Company getCompany() { return company; } public void setCompany(Company company) { this .company = company; } } |
(2)我们配置映射文件,我们在Company这一方设置外键来关联Address 有外键的一方要使用<many-to-one>元素来配置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Company.hbm.xml <hibernate-mapping > < class name= "com.cad.domain.Company" table= "company" > <id name= "id" column= "id" > <generator class = "native" ></generator> </id> <property name= "name" column= "name" ></property> <!--column指定外键,unique指定外键唯一约束,设为 true ,就可以表达Company和Address对象之间的一对一关联--> <many-to-one name= "address" class = "com.cad.domain.Address" column= "aid" unique= "true" ></many-to-one> </ class > </hibernate-mapping> Address.hbm.xml <hibernate-mapping > < class name= "com.cad.domain.Address" table= "address" > <id name= "id" column= "id" > <generator class = "native" ></generator> </id> <property name= "name" column= "name" ></property> <!--property-ref属性指定通过从Company的address属性来查找自己--> <one-to-one name= "Company" class = "com.cad.domain.Company" property-ref= "address" ></one-to-one> </ class > </hibernate-mapping> |
测试:
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 | public class Demo { private Session session; @Test public void test() { //读取配置文件 Configuration conf= new Configuration().configure(); //根据配置创建factory SessionFactory sessionfactory=conf.buildSessionFactory(); session = sessionfactory.openSession(); Transaction ts=session.beginTransaction(); Company c= new Company(); c.setName( "百度" ); Address a= new Address(); a.setName( "深圳" ); c.setAddress(a); a.setCompany(c); session.save(a); session.save(c); ts.commit(); session.close(); sessionfactory.close(); } } 默认情况下,一对一关联采用迫切左外连接检索策略。 |
三、按照主键映射
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 | 实体类不变,我们编写一下配置文件 address的表的id字段既是主键又是外键. 编写配置文件 Company.hbm.xml <hibernate-mapping > < class name= "com.cad.domain.Company" table= "company" > <id name= "id" column= "id" > <generator class = "native" ></generator> </id> <property name= "name" column= "name" ></property> <one-to-one name= "address" class = "com.cad.domain.Address" ></one-to-one> </ class > </hibernate-mapping> Address.hbm.xml <hibernate-mapping > < class name= "com.cad.domain.Address" table= "address" > <id name= "id" column= "id" > <!--必须使用foreign标识符生成策略,还要指定哪个对象共享OID--> <generator class = "foreign" > <param name= "property" >company</param> </generator> </id> <property name= "name" column= "name" ></property> <!--constrained属性设为 true ,说明主键同时作为外键--> <one-to-one name= "company" class = "com.cad.domain.Company" constrained= "true" ></one-to-one> </ class > </hibernate-mapping> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)