Hibernate one-to-one mapping

本文介绍了一对一关联映射在Hibernate中的实现方式,包括单向和双向映射的区别及配置方法,通过Java注解和XML两种形式进行了详细解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Uni-direction: only one class has a reference

Bidirection: both classes have the reference

 

1.Annotation

 

Uni-direction: no change to wife; wife_id is used as a foreign key to Husband;
@JoinColumn(name="name of the column in DB"), if we dont add this, by default, the column's name would be wife_id

@Entity
public class Husband {
        private int id;
        private String name;
        private Wife wi;
        
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        
        @OneToOne
        //name of the column if you want is different than default
        @JoinColumn(name="wifeId")
        public Wife getWife() {
            return wi;
        }
        public void setWife(Wife wife) {
            wi = wife;
        }
        
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
}

 

Bidirection: we add (mappedBy ="wife") to Wife class;in such way, we won't have husband_id in Wife class, only wife_id is in husband class.

@Entity
public class Wife {
    private int id;
    private String name;
    private Husband husband;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    //="wife" b/c the method is called getWife, we take"wife" after get ;not b/c private Wife wife
    @OneToOne(mappedBy="wife")
    public Husband getHusband() {
        return husband;
    }
    public void setHusband(Husband husband) {
        this.husband = husband;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 

2.XML

Unidirection:

Nothing special to Student class; StudentIdCard class has "id" from Student class as a foreign key

<many-to-one> should be on "many" side. StudentIDCard.hbm.xml should have <many-to-one name="student of getStudent()">; column ="" is optional;

We must have unique=true to keep the foreign key one-to-one relationship

<class name="Student" table="student" dynamic-update="true">
    <id name="id" column="id">
        <generator class="native"></generator>
    </id>
        <property name="name"></property>
    <property name="age"></property>
    <property name="classID"></property>
</class>


<class name="com.hibernate.model.StuIdCard">
     <id name="id">
        <generator class="native">
        </generator>
     </id>
    <property name="num"></property>
    <many-to-one name="student" column="studentId" unique="true"></many-to-one>
</class>

 

Bidirection:

Student class: contains<one-to-one> tag as well, in order to avoid foreign key in both classes, we add <property-ref ="xx">(xx is the Student variable in studentID class);

StuIdCard class: no change , same as uidirection;

<class name="Student" table="student" dynamic-update="true">
    <id name="id" column="id">
    <generator class="native"></generator>
    </id>
    <property name="name"></property>
    <property name="age"></property>
    <property name="classID"></property>
    <one-to-one name="stuIdCard" property-ref="student"></one-to-one>
</class>


<class name="com.hibernate.model.StuIdCard">
     <id name="id">
        <generator class="foreign">
          <param name="property">student</param>
        </generator>
     </id>
    <property name="num"></property>
<many-to-one name="student" column="studentId" unique="true"></many-to-one>
</class>

 

  

 

转载于:https://www.cnblogs.com/fifi043/p/4906108.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值