In this tutorial we will demonstrate how to set up a One-to-One Mapping using annotations and MySQL as database. We will use a Worker table which is mapped one-to-one with an Info table which contains the info about the workers.

 

hibernate one to one association hibernate one to one association hibernate one to one association

In a one-to-one relationship, each row in one database table is linked to 1 and only 1 other row in another table. So, in a one-to-one relationship between Worker and Info, each row in Worker Table is linked to another row in Info Table. The number of rows in Worker Table must thus be equal to the number of rows in Info Table.

What are the benefits of a One to One Relationship ? Apparentely this relationship does not really bring any design benefits: however supposing that the Info table contains lots of fields, most of which are seldom retrieved with a query, then introducing a One-To-One mapping would reduce the overhead of retrieving a large number of fields.

Here's the SQL script used to create the tables WORKER and INFO, including the constraints:

mysql> create table worker (worker_id int(11) primary key auto_increment, worker
_name varchar(50), worker_info int(11) not null);
Query OK, 0 rows affected (0.06 sec)

mysql> create table info (info_id int(11) primary key auto_increment not null, a
ddress varchar(50), city varchar(50), state varchar(50));
Query OK, 0 rows affected (0.01 sec)

mysql> alter table worker add CONSTRAINT uc UNIQUE (worker_info);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table worker add CONSTRAINT uc FOREIGN KEY (worker_info) REFERENCES
 info (info_id);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

And this is these are the corresponding Entities Worker and Info:

package com.sample;

import java.io.Serializable;
import javax.persistence.*;


@Entity
public class Info implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="info_id")
    private int infoId;

    private String address;

    private String city;

    private String state;

    //bi-directional one-to-one association to Worker
    @OneToOne
    @JoinColumn(name="info_id", referencedColumnName="worker_info")
    private Worker worker;

    public Info() {
    }

    public int getInfoId() {
        return this.infoId;
    }

    public void setInfoId(int infoId) {
        this.infoId = infoId;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Worker getWorker() {
        return this.worker;
    }

    public void setWorker(Worker worker) {
        this.worker = worker;
    }
    
}
package com.sample;

import java.io.Serializable;
import javax.persistence.*;



@Entity
public class Worker implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="worker_id")
    private int workerId;

    @Column(name="worker_name")
    private String workerName;

    //bi-directional one-to-one association to Info
    @OneToOne(mappedBy="worker")
    private Info info;

    public Worker() {
    }

    public int getWorkerId() {
        return this.workerId;
    }

    public void setWorkerId(int workerId) {
        this.workerId = workerId;
    }

    public String getWorkerName() {
        return this.workerName;
    }

    public void setWorkerName(String workerName) {
        this.workerName = workerName;
    }

    public Info getInfo() {
        return this.info;
    }

    public void setInfo(Info info) {
        this.info = info;
    }
    
}
0
0
0
s2smodern