• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

one to one mapping using Hibernate

January 19, 2011 By j2eereference 1 Comment

Here we will learn about one to one mapping with a simple example.

We have two tables person, personDetials and we need to make one to one relation between these two tables. The table structure is as below.

Person

 person_id
 person_name
 pesron_FK

personDetails

 person_id
 zipCode
 Job
 Salary

and corresping to each and every record in person table, there should be a record in the personDetails table.This is the one to one relation in this example.

Let us first create a Class for the personDetails table as below

Java
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
package oneToOne;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name="personDetails")
public class PersonDetails {
 
  private int person_id;
  private int zipCode;
  private String job;
  private int income;
 
  @Column(name="person_id")
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  public int getPerson_id() {
   return person_id;
  }
  public void setPerson_id(int person_id) {
   this.person_id = person_id;
  }
 
  @Column(name="zipCode")
  public int getZipCode() {
   return zipCode;
  }
  public void setZipCode(int zipCode) {
   this.zipCode = zipCode;
  }
  @Column(name="job")
  public String getJob() {
   return job;
  }
  public void setJob(String job) {
   this.job = job;
  }
  @Column(name="income")
  public int getIncome() {
   return income;
  }
  public void setIncome(int income) {
   this.income = income;
  }
}

Person table to Object mapping is as below.

Java
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
package oneToOne;
 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
 
@Entity
@Table(name="Person")
public class PersonVO
{
 
  private int person_id;
  private String personName;
  private PersonDetails details;
 
  @Column(name="person_id")
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  public int getPerson_id() {
   return person_id;
  }
  public void setPerson_id(int person_id) {
   this.person_id = person_id;
  }
 
  @Column(name="person_Name")
  public String getPersonName() {
   return personName;
  }
  public void setPersonName(String personName) {
   this.personName = personName;
  }
  @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
  @JoinColumn(name="person_id_FK")
  public PersonDetails getDetails() {
   return details;
  }
  public void setDetails(PersonDetails details) {
   this.details = details;
  }  
}

Here you can notice that we have created an object of the personDetail class and on top of the getter method we annoted as oneToOne

We have certain parameters for oneToOne annotation

1) cascadeType –

If you want to rempove the data from personDetails while removing data from person we can select cascadeType.REMOVE,
same way to refresh the data from personDetails while refresing data in the person we can select cascadeType.REFRESH .
likewise we have different cascadeTypes

 1. ALL
2. ERGE
3. PERSIST
4. REFERESH
5. REMOVE

Instead of selecting each and every one of these we can select ALL

ie, If we delete a person , the corresponding record should be deleted from personDetails, If i update
data in person corresponding data should be updated in personDetails table.

2)  There is another property called Fetch, FetchType can have values either EAGER or LAZY

If we select EAGER , while fetching the person record, the corresponding record of the personDetails also get loaded

By default fetchType for OneToOne mapping is EAGER. But for oneToMany and ManyToMany default type is LAZY

3)  joincolumn– name for the join column, Normally postfix with _fk , for better understanding/readability.

Now lets see how can we insert data to these tables .

Java
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
  package oneToOne;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
 
public class testHibernate {
public static void main(String[] args) {  
  AnnotationConfiguration config=new AnnotationConfiguration();
  config.addAnnotatedClass(PersonDetails.class);  
  config.addAnnotatedClass(PersonVO.class);
  SessionFactory sessionFactory = config.configure().buildSessionFactory();    
  Session session = sessionFactory.openSession();
 
  Transaction tx=session.beginTransaction();
  PersonVO personVO=new PersonVO();
  personVO.setPersonName("John");
  PersonDetails personDetails=new PersonDetails();
  personDetails.setIncome(1000);
  personDetails.setJob("Junior Engineer");
  personDetails.setZipCode(21009);
  personVO.setDetails(personDetails);
  session.save(personVO);
  tx.commit();
   }
}

 

Since the cascade type is all , we dont have to save the details object separatly..

Related Posts

  • First level caching in Hibernate
  • Hibernate – Query By Criteria
  • Hibernate Query Language
  • Many to Many mapping in hibernate
  • One To Many mapping using Hibernate
  • Implementing composite primary key with JPA and Hibernate
  • How to implement primary key with JPA and Hibernate
  • What is Hibernate?

Filed Under: Hibernate

Reader Interactions

Comments

  1. sasikanth says

    May 22, 2012 at 1:42 pm

    very useful info about each and every point

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.