This mapping tells about many-to-many mapping. There are two tables, one is "Customer" and other is "Policy".

Customer table

Custmer_id Policy_name Amout
1 Pension plan 40000
1 Child plan 50000
2 Study plann 20000
3 Pension plan 40000

Policy table

Policy_Id Policy_Name
1 Pension plan
2 Child Plan
3 Study Plan

Bridge table i.e customer_policy

Policy_Id Policy_Name
1 Pension plan
2 Clild plan
3 Study plan

 

One Customer can go through more than one policy and the same policy can be done by different Customer. Like in this table Customer_id no 1 has two policy and same pension plan once again done by Customer_id no 3.This is a many to many relationship.

For achieving this relationship we have to create a bridge table (Customer_Policy)  through mapping, which maintains both keys i,e custmer_id and policy_id.

Steps are same as before we have discussed
Step1: Create  Custmer and Policy class

Customer.java.

package com.bullraider.manytomany;
import java.util.HashSet;
import java.util.Set;
public class Customer {
    private String name;
    private long custmerId;
    private Set<Policy> policyies = new HashSet<Policy>(0);
    public Set<Policy> getPolicyies() {
        return policyies;
    }

    public void setPolicyies(Set<Policy> policyies) {
        this.policyies = policyies;
    }

    public Customer(String name) {

        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getCustmerId() {
        return custmerId;
    }

    public void setCustmerId(long custmerId) {
        this.custmerId = custmerId;
    }


}

 

Policy.java

package com.bullraider.manytomany;
public class Policy {
    private long policyId;
    private String policyName;
        public long getPolicyId() {
        return policyId;
       
    }
    public void setPolicyId(long policyId) {
        this.policyId = policyId;
    }

    public Policy( String policyName) {
        super();
        this.policyName = policyName;
    }
    public String getPolicyName() {
        return policyName;
    }
    public void setPolicyName(String policyName) {
        this.policyName = policyName;
    }

}

Step2: Create Customer.hbm.xml and Policy.hbm.xml

Custmer.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.manytomany.Customer" table="customer">
        <meta attribute="class-description"> This class contains Customer details. </meta>
        <id name="custmerId" type="long" column="custmerId">
            <generator class="native" />
        </id>
        <property name="name" type="string" not-null="true" />
        <set name="policyies" table="customer_policy" cascade="all" >
            <key column="custmer_Id" />
            <many-to-many column="policy_Id" class="com.bullraider.manytomany.Policy" />
        </set>
       
            </class>
</hibernate-mapping>

Policy.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.manytomany.Policy" table="policy">
        <meta attribute="class-description"> This class contains policy details. </meta>
        <id name="policyId" type="long" column="policyId">
            <generator class="native" />
        </id>
        <property name="policyName" type="string" 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">cat</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/manytomany/Custmer.hbm.xml" />
        <mapping resource="com/bullraider/manytomany/Policy.hbm.xml" />
    </session-factory>
</hibernate-configuration>


step4: Create HibernateUtil class to get SessionFactory Object

package com.bullraider.manytomany.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.manytomany;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.bullraider.manytomany.util.HibernateUtil;

public class Main {


    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            Set<Policy> policySet = new HashSet<Policy>();
            policySet.add(new Policy("JeevanSalal"));
            policySet.add(new Policy("Retirementplan"));
            Customer c1=new Customer("Abbi");
            c1.setPolicyies(policySet);
            Customer c2=new Customer("Mita");
            c2.setPolicyies(policySet);
            session.save(c1);
            session.save(c2);
            transaction.commit();
            System.out.println("Records inserted sucessessfully");
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }

    }

}

Note:

<set name="policyies" table="customer_policy" cascade="all"  >
            <key column="custmer_Id" />
            <many-to-many column="policy_Id" class="com.bullraider.m2m.Policy" />
        </set>

Here we are generating bridge table " Custmer_policy" which have two attribute one is "Custmer_id" and "Policy_id"

download zip