Many-to-One is another relationship. Here we are describing this relationship through Employee and Dept table.

Employee Table

empno ename job sal deptno
1 Deepak SALESMAN 13444 20
2  Sandeep

CLERK

33336 10
3 Guddu MGR 20000 20
4 Tripti MGR 40000 30

Dept  table

id deptno
1 10
2 20
3 30

Same deptno can assigned to one or more than one employee. So many Empoyee has same deptno.Like empno 1,3 have assigned to deptno 10, empno2,4,5 are under deptno 20. So this is a many-to-one relationship.

Following are the steps to achieving it.

Step1: Create  Employee and Department Pojo

Employee.java

package com.bullraider.manytoone;
import java.io.Serializable;
public class Employee implements Serializable{
    private long empno;
    private String ename;
    private int sal;
    private String job;
    private int deptno ;
    private Department dept;
    public Employee( String ename, String job, int sal,int deptno) {
        super();
        this.deptno = deptno;
        this.ename = ename;
        this.job = job;
        this.sal = sal;
    }
    
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public int getSal() {
        return sal;
    }
    public void setSal(int sal) {
        this.sal = sal;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public int getDeptno() {
        return deptno;
    }
    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }
    public Department getDept() {
        return dept;
    }
    public void setDept(Department dept) {
        this.dept = dept;
    }

    public long getEmpno() {
        return empno;
    }

    public void setEmpno(long empno) {
        this.empno = empno;
    }
  }

 

Department.java

package com.bullraider.manytoone;
public class Department {
    
    private long id;
    private int deptno;
    public Department(int id) {
        this.id=id;
       
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public int getDeptno() {
        return deptno;
    }
    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }
    }

 


Step2: Create Employee.hbm.xml and Department.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.bullraider.manytoone.Employee"
        table="emp110">
        <meta attribute="class-description"> This class contains employee details. </meta>
        <id name="empno" type="long" column="empid">
            <generator class="native" />
        </id>
        <property name="ename" type="string" column="ename" not-null="true" />
        <property name="job" type="string" column="job" not-null="true" />
        <property name="sal" type="integer" column="sal" not-null="true" />
        <property name="deptno" type="integer" column="deptno"
            not-null="true" />
        <many-to-one name="dept" class="com.bullraider.manytoone.Department" column="id"

cascade="all" />
    </class>
</hibernate-mapping>

 

 

Deptarment.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.bullraider.manytoone.Department" table="dept_emp110">
        <meta attribute="class-description"> This class contains department details. </meta>
        <id name="id" type="long" column="id">
            <generator class="native" />
        </id>
        <property name="deptno" type="integer" not-null="true" />
       
    </class>
</hibernate-mapping>

 


Step3: Create Configuration file

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="connection.password">tiger</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="show_sql">true</property>
       <property name="hbm2ddl.auto">create-drop</property>
     <mapping resource="com/bullraider/manytoone/Department.hbm.xml"/>
        <mapping resource="com/bullraider/manytoone/Employee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

step4: Create HibernateUtil class to get SessionFactory Object

package com.bullraider.manytoone.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("SessionFactory creation failed" + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

 


step5: Create One Main Class for adding opertions.

package com.bullraider.manytoone;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.bullraider.manytoone.util.*;
public class Main {

    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            Department dept1=new Department(10);
            Employee employee1=new Employee("Pradeep","SALESMAN",12000,10);
            Department dept2=new Department(20);
            Employee employee2=new Employee("Raja","MGR",124000,20);
            session.save(dept1);
            session.save(employee1);
            session.save(dept2);
            session.save(employee2);
            transaction.commit();
            System.out.println("Records inserted sucessessfully");
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }

    }
}



download zip