Pages

Sunday 2 June 2013

 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
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;
 }

}
2)AuthorContactDetails class
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;
}
}

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




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());
 }

}
now run the above program we will get the below output

name of the author is kb

No comments:

Post a Comment