Hibernate Left Join, Hibernate Left Join Example

Hibernate » on Aug 10, 2011 { 10 Comments } By Sivateja

Left join means, the objects from both sides of the join are selected and more objects  from left side are selected, even though no equal objects are there at right side, no confusion you will be able to understand if you go through this example i guess 🙂

Let us see an example on hibernate left join, am taking one-to-many to explain this concept files required….

  • Vendor.hbm.xml
  • Customer.hbm.xml
  • hibernate.cfg.xml

package str;

import java.util.Set;

public class Vendor {

	private int vendorId;
	private String vendorName;
	private Set children;

	public int getVendorId() {
		return vendorId;
	public void setVendorId(int vendorId) {
		this.vendorId = vendorId;
	public String getVendorName() {
		return vendorName;
	public void setVendorName(String vendorName) {
		this.vendorName = vendorName;
	public Set getChildren() {
		return children;
	public void setChildren(Set children) {
		this.children = children;


package str;
public class Customer {

	private int customerId;
	private String customerName;
	private int forevenId;

	public int getCustomerId() {
		return customerId;
	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	public String getCustomerName() {
		return customerName;
	public void setCustomerName(String customerName) {
		this.customerName = customerName;

	public int getForevenId() {
		return forevenId;
	public void setForevenId(int forevenId) {
		this.forevenId = forevenId;



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<class name="str.Vendor" table="vendor">

<id name="vendorId" column="vendid"  />
<property name="vendorName" column="vendname" length="10"/>

<set name="children" cascade="all" lazy="false">

<key column="forevenid" />
<one-to-many class="str.Customer"/>




<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<class name="str.Customer" table="customer">

<id name="customerId" column="custid"  />
<property name="customerName" column="custname" length="10"/>

<property name="forevenId" column="forevenid"  insert="false" />



<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
<property name="connection.url"></property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="">update</property>

<mapping resource="Customer.hbm.xml"></mapping>
<mapping resource="Vendor.hbm.xml"></mapping>


package str;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class urLogic {

	public static void main(String args[])

		Configuration cfg = new Configuration();

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();		

		Query qry= session.createQuery("select v.vendorName, c.customerName from Vendor v Left Join v.children c");

		List l = qry.list();
		Iterator it=l.iterator();

			Object rows[] = (Object[]);
			System.out.println(rows[0]+ " -- " +rows[1]);



  • Actually in see line number 27, i have written select v.vendorName, c.customerName from Vendor v Left Join v.children c
  • See before Left Join key word, i selected from Vendor v, after Left Join keywork v.children c
  • Means, actually we know in one to many if we select the data from parent then, we will get the data automatically from the children table, i mean records mapping with the parent table, hope you remembered 🙂
  • Let in parent we have 5 records, and in children we have 3 records.  And 2 records are having relation with children table records

Before execute this program…..

Out put once we executed……


And thats it mates.., try with Right Join and remaining joins just same concept.


You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


10 Responses to “Hibernate Left Join, Hibernate Left Join Example”
  1. Rawoof says:

    you wrote insted of customer.xml

  2. Titin says:

    What is the use of CallBack interface in hibernate?

    • Salman Khan says:

      The use of callback method in hibernate is basically for increase the performance.
      Suppose, If we are not using callback method and if we call a method in another method then in this case first we have to execute first method and then execute calling method. or we can say that in calling method whatever method we are calling of that method code will be come.
      And If We are using callback method then first code will be executed in the method then we have to call only response of that method. Becoz of this definitely performance will be improved.

  3. Gokulnathan says:

    It is private Set Childern instead private Set Customer. Respectively the setter & getter methods. Change it!

  4. mateen says:

    db schema is missing

  5. Mukesh says:

    what is this (Join v.children c) in query

  6. Bhujatha Reddy says:

    How to make relationship between three tables using hibernate,
    Ex, i have one Account tale with fields accno,name, balance
    and usertable with fields user_id, account_id, and third table is movie_ticket with fields ticket_I’d, ticket_price,ticket_count

    Can you please help me how to deduct balance and ticket count when i book tucket

    Thank u

  7. Navya says:

    left join in that vendor.xml is there but that is vendor.hbm.xml file
    and but customer.hbm.xml right

  8. harikrishna says:

    Hi Siva garu,

    I think in the join query we must include "left outer join customer on v.vendid=c.custid". Correct me if am wrong.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.