Hibernate Left Join Example

By |2019-04-14T21:19:35+05:30May 2nd, 2017|Hibernate|

In this tutorial, I am going to show you how to work with Hibernate Left Join.

What is Left Join?

The Left Join is a keyword in SQL, which returns all data from the left-hand side table and matching records from the right-hand side table. We can see the NULL values from the right side if there is no match.

Hibernate Left Join :

We can apply the Joins in Hibernate by using the HQL query or native SQL query. To make a join between the two tables, the two tables must be in a logical relationship. We can achieve the relationship between two tables by applying the parent table’s primary key as a child table’s foreign key.

Hibernate Left Join Example :

For this tutorial, I am going to take the two tables like Customer and Items; these two are having one to many relationship. That means one Customer can have multiple Items.

Project Structure :

Hibernate Left oin Create customer and item tables with data to apply left join.

Customer Table:Hibernate Left Join Customer Create item Table:Hibernate Left Join Item Customer.java

package com.otp.hibernate.pojo;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Table(name = "customer")
public class Customer {
    @Column(name = "cid")
    private int customerId;
    @Column(name = "cname", length = 15)
    private String customerName;
    @Column(name = "ccity", length = 20)
    private String customerCity;

    @OneToMany(targetEntity = Item.class, cascade = CascadeType.ALL)
    @JoinColumn(name = "cid",referencedColumnName="cid")
    private List items;

    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 String getCustomerCity() {
        return customerCity;

    public void setCustomerCity(String customerCity) {
        this.customerCity = customerCity;

    public List getItems() {
        return items;

    public void setItems(List items) {
        this.items = items;


Create Item Class

package com.otp.hibernate.pojo;

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

public class Item {
    private int itemId;
    private String itemName;
    private int price;

    public int getItemId() {
        return itemId;

    public void setItemId(int itemId) {
        this.itemId = itemId;

    public String getItemName() {
        return itemName;

    public void setItemName(String itemName) {
        this.itemName = itemName;

    public int getPrice() {
        return price;

    public void setPrice(int price) {
        this.price = price;


Create Hibernate Configuration File :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

Create Hibernate Utility Class

package com.otp.hibernate.pojo;

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

public class HibernateUtility{
    private static SessionFactory factory;

    private HibernateUtility() {

    public synchronized static SessionFactory getSessionFactory() {
        if (factory == null) {
            factory = new Configuration().configure("hibernate.cfg.xml")
        return factory;

    protected Object clone() throws CloneNotSupportedException {
        return new RuntimeException("Clone not Supported");


Run the application :

Query For Hibernate Left Join on the above tables, the same will be implemented in below class as hql select query.

 select c.customerName, c.customerCity, i.itemName,i.price from Customer c left join c.items i;


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

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

import com.otp.hibernate.pojo.HibernateUtility;

public class Main {

    public static void main(String[] args) {
        SessionFactory factory = HibernateUtility.getSessionFactory();
        Session session = factory.openSession();
        Query qry= session.createQuery("select c.customerName, c.customerCity, i.itemName,i.price from Customer c "
                + "left join c.items i");
        List l = qry.list();
        Iterator it=l.iterator();
            Object rows[] = (Object[])it.next();
            System.out.println(rows[0]+ " -- " +rows[1] + "--"+rows[2]+"--"+rows[3]);



Hibernate Left Join Output

Happy Learning 🙂

Download Example

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment