Here we are defining left-outer join between customer and policy tables based on customer_id.

If you want to know about left outer join click here

These are the steps

 

  1. Write down the Customer bean classs.
    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;
    	}
    
    
    }
    
    
  2. This is the Customer.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>
    
  3. This is the Policy Class to get specially SessionFactory object
    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;
    	}
    
    }
    
    
  4. This is the 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>
    
  5. Design the HibernateUtil  class
    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;
    	}
    }
    
    
  6. Design the Main Class
    package com.bullraider.manytomany;
    
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    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");
    			Main m1=new Main();
    			m1.retrivePolicy();
    		} catch (HibernateException e) {
    			transaction.rollback();
    			e.printStackTrace();
    		} finally {
    			session.close();
    		}
    
    	}
    	public void retrivePolicy() 
    
    	{ 
    
    		Session session = HibernateUtil.getSessionFactory().openSession(); 
    		Transaction transaction = null; 
    		try { 
    			transaction = session.beginTransaction(); 
    						List policy = session.createQuery("select from Customer c.customerId left join Policy p.policyId  on c.customerId=p.policyId").list(); 
    						
    						/*from Record rec
    						left join rec.Values vals with vals.index = :field
    						order by vals*/
    			
    			for (Iterator iterator = policy.iterator(); iterator.hasNext();) 
    			{ 
    				Customer customer  = (Customer) iterator.next(); 
    				 
    			System.out.println(customer.getCustmerId());
    			} 
    			
    			transaction.commit(); 
    
    		} catch (HibernateException e) { 
    
    			transaction.rollback(); 
    
    			e.printStackTrace(); 
    
    		} finally { 
    
    			session.close(); 
    
    		} 
    	}
    
    }
    
    
  7. Design the hibernate.cfg.xml
    <?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>
    

download left-outer-join.zip