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
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.
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 .
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..
sasikanth says
May 22, 2012 at 1:42 pmvery useful info about each and every point