Hibernate关联关系映射之一对一关联关系

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate关联关系映射之一对一关联关系相关的知识,希望对你有一定的参考价值。

人和身份证之间就是一个典型的一对一关联关系。实现一对一关联关系映射的方式有两种一种是基于外键,一种是基于主键,下面我们先看基于外键的关联方式

首先看他们的实体类

Person类

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
package entity;
 
public class Person {
    private Integer id;
    private String name;
    private IdCard idCard;
    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 IdCard getIdCard() {
        return idCard;
    }
    public void setIdCard(IdCard idCard) {
        this.idCard = idCard;
    }
}

然后看IdCard类

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
package entity;
 
public class IdCard {
    private Integer id;
    private String number;
    private Person person;
     
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
     
     
}

这里在Person类和IdCard类中分别有一个对方类型的属性。

然后看他们之间的关联关系映射,首先看Person.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 
<hibernate-mapping>
    <class name="entity.Person" table="Person" schema="MYHR">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="NAME" not-null="true" />
        </property>
        <!-- idCard属性,IdCard类型,表达本类与IdCard的一对一关联 -->
        <!-- 采用基于外键的一对一,本方无外键方 -->
        <!-- property-ref属性:对方映射中外键类对应的属性名 -->
        <one-to-one name="idCard" class="entity.IdCard" property-ref="person">
        </one-to-one>
    </class>
</hibernate-mapping>

然后看一下IdCard.hbm.xml文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping>
    <class name="entity.IdCard" table="IdCard" schema="MYHR">
        <id name="id">
            <column name="ID"/>
            <generator class="assigned" />
        </id>
        <property name="number">
            <column name="NAME" not-null="true" />
        </property>
        <!-- person属性,Person类型,表示与Person类的一对一关联关系 -->
        <!-- 采用基于外键的一对一关联方式,本方有外键 -->
        <many-to-one name="person" column="personId" class="entity.Person" unique="true"></many-to-one>
    </class>
</hibernate-mapping>

这样Person和IdCard之间的一对一关联关系就搭建好了,然后看一下测试类

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package test;
 
import static org.junit.Assert.*;
 
import org.hibernate.Session;
import org.hibernate.Transaction;
 
import entity.IdCard;
import entity.Person;
import factory.HibernateSessionFactory;
 
public class Test {
 
    private Session session = null;
    private Transaction tran = null;
    @org.junit.Test
    public void save() {
        session = HibernateSessionFactory.getSession();
        tran = session.beginTransaction();
        try{
            //新建对象
            Person p = new Person();
            p.setId(1);
            p.setName("张三");
             
            IdCard idCard = new IdCard();
            idCard.setId(1);
            idCard.setNumber("10001");
             
            p.setIdCard(idCard);
            idCard.setPerson(p);
             
            session.save(p);
            session.save(idCard);
             
            tran.commit();
        }catch(Exception e){
            tran.rollback();
        }
    }
 
    @org.junit.Test
    public void Get() {
        session = HibernateSessionFactory.getSession();
        tran = session.beginTransaction();
        try{
            Person person = (Person) session.get(Person.class, 1);
            IdCard idCard = person.getIdCard();
            System.out.println(person.getName()+"身份证号:"+idCard.getNumber());
            tran.commit();
        }catch(Exception e){
            tran.rollback();
        }
    }
    //解除关系
    @org.junit.Test
    public void RemoveRelation() {
        session = HibernateSessionFactory.getSession();
        tran = session.beginTransaction();
        try{
            //从有外键方接触关联关系可以,不可以从无外键方解除
            IdCard idCard = (IdCard)session.get(IdCard.class, 1);
            idCard.setPerson(null);
            tran.commit();
        }catch(Exception e){
            tran.rollback();
        }
    }
    //删除
    @org.junit.Test
    public void DeleteRelation() {
        session = HibernateSessionFactory.getSession();
        tran = session.beginTransaction();
        try{
            //如果没有关联对方,可以直接删除。
            //如果有关联对方,且可以维护关联关系,他就会先删除关联关系,在删除自己。
            //如果有关联关系,且不能维护关联关系,所以会执行删除自己,并且抛出异常。
            IdCard idCard = (IdCard)session.get(IdCard.class, 1);
            session.delete(idCard);
            tran.commit();
        }catch(Exception e){
            tran.rollback();
        }
    }
}

这就是基于外键方式,基于主键的方式只需要在以上基础修改一下映射文件即可,下面分别看一下修改后的映射文件

首先看Person.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 
<hibernate-mapping package="entity">
    <class name="entity.Person" table="Person" schema="MYHR">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="NAME" not-null="true" />
        </property>
        <!-- idCard属性,IdCard类型,表达本类与IdCard的一对一关联 -->
        <!-- 采用基于主键的一对一,本方无外键方 -->
        <one-to-one name="idCard" class="entity.IdCard"/>
       
    </class>
</hibernate-mapping>

然后看IdCard.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping package="entity">
    <class name="entity.IdCard" table="IdCard" schema="MYHR">
        <id name="id" type="int">
            <column name="ID" />
            <!-- 当使用基于主键的一对一映射时,有外键方的主键生成策略是foreign, -->
            <!-- 参数property:生成主键值时所根据的对象 -->
            <generator class="foreign">
                <param name="property">person</param>
            </generator>
        </id>
        <property name="number" type="string">
            <column name="NAME" not-null="true" />
        </property>
        <!-- person属性,Person类型,表示与Person类的一对一关联关系 -->
        <!-- 采用基于主键的一对一关联方式,本方有外键 -->
        <one-to-one name="person" class="entity.Person"
            constrained="true" />
    </class>
</hibernate-mapping>

以上是关于Hibernate关联关系映射之一对一关联关系的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate之关联关系映射(一对一主键映射和一对一外键映射)

Mybatis之关联关系(一对多多对多)

hibernate基础07:关联关系映射之基于外键单向一对一

hibernate的映射关系之一对多

hibernate学习---关联关系映射

Hibernate 映射一对一关联关系