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.



    <property name="">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"/>

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;

package com.javainfinite.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


public class Address implements Serializable {
    private int address1;
    private String city;
    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) { = city;

    public String getState() {
        return state;

    public void setState(String state) {
        this.state = state;

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;

public class Employee {
    private int eid;
    private String ename;
    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;

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


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





By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *