OneToMany in Hibernate using Annotations
Here we are going to implement OneToMany mapping in hibernate using Annotations.
We are going to have 2 entities Address and Employee. Here we are assuming that an employee can have more than one address. When we enter the employee details and his addresses (Here we are going to enter 2 addresses for one employee) a 3rd table creating which brings the relationship between the both Employee and Address table.
Once we go through the example, the concept will be clear.
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 java.util.ArrayList; import java.util.List; 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.JoinTable; import javax.persistence.OneToMany; 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; @OneToMany (cascade={CascadeType.ALL}) @JoinTable(name="Joined_Table", joinColumns=@JoinColumn(name="EmployeeID_Mapped"), inverseJoinColumns=@JoinColumn(name="Address_Mapping")) private List<Address> address=new ArrayList(); public List<Address> getAddress() { return address; } public void setAddress(List<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(); Address addr1=new Address(); emp.setEname("Alpha"); emp.setEdept("Model"); addr.setState("Anatimec"); addr.setCity("Gotham"); addr1.setCity("Venice"); addr1.setState("Ven"); emp.getAddress().add(addr); emp.getAddress().add(addr1); session.save(emp); trans.commit(); } }
We can see 3 tables has been created, Employee_Mapping, Address_Mapping and Joined_Table. Here the relationship between the Employee_Mapping and Address_Mapping is pointed out in JoinedTable
we can see that employee_ID – 3, that employee has 2 address that are mapped in address table (5,6)
This is know an OneToMany Mapping