One To One Mapping in Hibernate

Using hibernate, if we want to put relationship between two entities means two Pojo classes, then in the database tables, there must exist foreign key relationship, we call it as Referential integrity.

there are many ways to do the mapping in Hibernate. lets see one of such way “one to one “.

we have 2 entities here 1)Author 2)AuthorContactDetails

we need to establish one to one relationship between these 2 entities now. means one author ms have one contact details .

in DB we will have 2 separate tables one for author and other one for author contact details.

in java also we will have 2 separate classes for each of the above tables.

then how can we give the associations between these 2 tables.  lets do it using hibernate annotation.

1)Author class

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
<code>package com.kb.model;
 
import javax.persistence.Entity;
 
@Entity
public class Author {
 @Id
 @GeneratedValue
 private int authodNo;
 private String name;
 private String aboutAuthor;
 @OneToOne
 @JoinColumn(name="Author_Contact_Id")
 private AuthorContactDetails contactDetails;
 
 public int getAuthodNo() {
  return authodNo;
 }
 
 public void setAuthodNo(int authodNo) {
  this.authodNo = authodNo;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public String getAboutAuthor() {
  return aboutAuthor;
 }
 
 public void setAboutAuthor(String aboutAuthor) {
  this.aboutAuthor = aboutAuthor;
 }
 
 public void setContactDetails(AuthorContactDetails contactDetails) {
  this.contactDetails = contactDetails;
 }
 
 public AuthorContactDetails getContactDetails() {
  return contactDetails;
 }
 
}</code>
<code>package com.kb.model;

import javax.persistence.Entity;

@Entity
public class Author {
 @Id
 @GeneratedValue
 private int authodNo;
 private String name;
 private String aboutAuthor;
 @OneToOne
 @JoinColumn(name="Author_Contact_Id")
 private AuthorContactDetails contactDetails;

 public int getAuthodNo() {
  return authodNo;
 }

 public void setAuthodNo(int authodNo) {
  this.authodNo = authodNo;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAboutAuthor() {
  return aboutAuthor;
 }

 public void setAboutAuthor(String aboutAuthor) {
  this.aboutAuthor = aboutAuthor;
 }

 public void setContactDetails(AuthorContactDetails contactDetails) {
  this.contactDetails = contactDetails;
 }

 public AuthorContactDetails getContactDetails() {
  return contactDetails;
 }

}</code>

2)AuthorContactDetails class

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
<code>package com.kb.model;
 
import javax.persistence.Entity;
 
@Entity
public class AuthorContactDetails {
@Id
@GeneratedValue
private int id;
private String email;
private String mobile;
public int getId() {
 return id;
}
public void setId(int id) {
 this.id = id;
}
public String getEmail() {
 return email;
}
public void setEmail(String email) {
 this.email = email;
}
public String getMobile() {
 return mobile;
}
public void setMobile(String mobile) {
 this.mobile = mobile;
}
}</code>
<code>package com.kb.model;

import javax.persistence.Entity;

@Entity
public class AuthorContactDetails {
@Id
@GeneratedValue
private int id;
private String email;
private String mobile;
public int getId() {
 return id;
}
public void setId(int id) {
 this.id = id;
}
public String getEmail() {
 return email;
}
public void setEmail(String email) {
 this.email = email;
}
public String getMobile() {
 return mobile;
}
public void setMobile(String mobile) {
 this.mobile = mobile;
}
}</code>

look at the Author class very closely, we can find the association between them.

we have used @onetoone annotation to indicate hibernate that this class has one dependent table in the DB.

and please do the mapping(take primary key of that contact table and put in the author table as a foreign key)

so now mapping is done between these 2 tables. whenever we persist the author object it expects contact details to be persisted in the DB.

so persist contact details  and author and leave the mapping for hibernate, then we can see one column which is used as a mapping between 2 tables.

see below program for the same

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
<code>package com.kb.dao;
 
import org.hibernate.Session;
 
public class OneToOne {
 
 public static void main(String[] args) {
  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  Author author = new Author();
  author.setName("kb");
  author.setAboutAuthor("good one");
 
  AuthorContactDetails contactDetails = new AuthorContactDetails();
  contactDetails.setEmail("abc@abcd.com");
  contactDetails.setMobile("1234567891");
 
  author.setContactDetails(contactDetails);
 
  try {
   session.beginTransaction();
   session.save(author);
   session.save(contactDetails);
   session.getTransaction().commit();
  } catch (Exception e) {
  session.getTransaction().rollback();
  }
  finally{
   session.close();
  }
 
  session=sessionFactory.openSession();
  author = (Author)session.load(Author.class, 1);
 
  System.out.println("name of the author is "+author.getName());
 }
 
}</code>
<code>package com.kb.dao;

import org.hibernate.Session;

public class OneToOne {

 public static void main(String[] args) {
  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  Author author = new Author();
  author.setName("kb");
  author.setAboutAuthor("good one");

  AuthorContactDetails contactDetails = new AuthorContactDetails();
  contactDetails.setEmail("abc@abcd.com");
  contactDetails.setMobile("1234567891");

  author.setContactDetails(contactDetails);

  try {
   session.beginTransaction();
   session.save(author);
   session.save(contactDetails);
   session.getTransaction().commit();
  } catch (Exception e) {
  session.getTransaction().rollback();
  }
  finally{
   session.close();
  }

  session=sessionFactory.openSession();
  author = (Author)session.load(Author.class, 1);

  System.out.println("name of the author is "+author.getName());
 }

}</code>

now run the above program we will get the below output

name of the author is kb

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on