OneToOne Mapping in Hibernate using Annotations:
In this example lets see how to implement OneToOne mapping in hibernate using Annotations.
There are 3 types of Mappings in Hibernate,
1. OneToOne
2. ManyToOne
3.ManyToMany
Here lets see an example of OntToOne Mapping.
In our example we are going to have 2 entities Address and Employee. When we enter the employee details there will a column that points to another table ID where the related address of that employee is stored.
Once you go through this example, you can understand clearly.
hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateannotation</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <mapping class="com.javainfinite.pojo.Employee"/> <mapping class="com.javainfinite.pojo.Address"/> </session-factory> </hibernate-configuration>
NewHibernateUtil.java
package com.javainfinite.pojo; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.SessionFactory; public class NewHibernateUtil { private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from standard (hibernate.cfg.xml) // config file. sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Throwable ex) { // Log the exception. System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
Address.java
package com.javainfinite.pojo; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="Address_Mapping") public class Address implements Serializable { @Id @GeneratedValue private int address1; private String city; @Column(name="State_EMP") private String state; public int getAddress1() { return address1; } public void setAddress1(int address1) { this.address1 = address1; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
Employee.java
package com.javainfinite.pojo; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="Employee_Mapping") public class Employee { @Id @GeneratedValue @Column(name="Employee_ID") private int eid; @Column(name="Employee_Name") private String ename; @Column(name="Employee_Dept") private String edept; @OneToOne (cascade={CascadeType.ALL}) @JoinColumn(name="Address_Joining") private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public int getEid() { return eid; } public void setEid(int eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getEdept() { return edept; } public void setEdept(String edept) { this.edept = edept; } }
Execution.java
package com.javainfinite.execution; import com.javainfinite.pojo.Address; import com.javainfinite.pojo.Employee; import com.javainfinite.pojo.NewHibernateUtil; import org.hibernate.Session; import org.hibernate.Transaction; public class Execution { public static void main(String args[]) { Session session=NewHibernateUtil.getSessionFactory().openSession(); Transaction trans=session.beginTransaction(); Employee emp=new Employee(); Address addr=new Address(); emp.setEname("Alpha"); emp.setEdept("Model"); addr.setState("Venganza"); addr.setCity("Veidsa"); emp.setAddress(addr); session.save(emp); trans.commit(); } }
Output:
Here we can see that a new column “Address_Joining” has been included in table and it points to ID: 1 of Address table
Here we can see the address table with pointing id:1