一、hbm.xml

1.CardEntity.hbm.xml

 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 <hibernate-mapping package="com.sdkj.hibernate.domain">
 6 
 7     <class name="HCardEntity" table="h_card" schema="678">
 8         <id name="cid" type="int">
 9             <column name="cid"/>
10             <generator class="native"/>
11         </id>
12         <property name="cardnum" type="java.lang.String" column="cardnum"/>
13 
14         <one-to-one name="hPersonEntity" class="HPersonEntity" property-ref="hCardEntity"></one-to-one>
15         <!-- 主键 -->
16     </class>
17 </hibernate-mapping>

2.PersonEntity.hbm.xml

 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 <hibernate-mapping package="com.sdkj.hibernate.domain">
 6 
 7     <class name="HPersonEntity" table="h_person" schema="678">
 8         <id name="pid" type="int">
 9             <column name="pid"></column>
10             <generator class="native"/>
11         </id>
12         <property name="pname" column="pname"/>
13 
14         <many-to-one name="hCardEntity" class="HCardEntity">
15             <column name="cardid" unique="true"></column>
16         </many-to-one>
17         <!-- 多对一加上唯一性约束 实质上就变成一对一了 -->
18     </class>
19 </hibernate-mapping>

二、TestOneToOne

 1 package com.sdkj.hibernate.controller;
 2 
 3 import com.sdkj.hibernate.domain.HCardEntity;
 4 import com.sdkj.hibernate.domain.HPersonEntity;
 5 import com.sdkj.hibernate.util.CommonUtil;
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 import org.hibernate.Transaction;
 9 import org.junit.jupiter.api.Test;
10 
11 /**
12  * @Author wangshuo
13  * @Date 2022/4/8, 21:13
14  * Please add a comment
15  */
16 public class TestOneToOne extends CommonUtil {
17 
18     @Test
19     public void test(){
20 
21         SessionFactory sessionFactory = getSessionFactory();
22         Session session = sessionFactory.openSession();
23         Transaction transaction = session.beginTransaction();
24 
25         HPersonEntity hPersonEntity = new HPersonEntity();
26         hPersonEntity.setPname("小王");
27 
28         HCardEntity hCardEntity = new HCardEntity();
29         hCardEntity.setCardnum("66666666");
30 
31         hCardEntity.sethPersonEntity(hPersonEntity);
32         hPersonEntity.sethCardEntity(hCardEntity);
33         session.save(hCardEntity);
34         session.save(hPersonEntity);
35 
36         HPersonEntity load = session.load(HPersonEntity.class, 1);
37         System.out.println(load);
38         //此时不会发起关联查询,还是因为懒加载机制
39         HCardEntity hCardEntity1 = load.gethCardEntity();
40         System.out.println(hCardEntity1);
41         //此时需要用card属性时才会发起关联查询
42 
43         //删除时要注意,删Person(多端随便删) 删一端需要先把多端对应数据删除或修改才可以删
44         session.delete(hCardEntity);
45         commit(transaction,sessionFactory,session);
46     }
47 }