Share on Google+Share on Google+

Hibernate Left Outer Join

In this section, you will learn how to do Left Outer Join in Hibernate.

Hibernate Left Outer Join

In this section, you will learn how to do Left Outer Join in Hibernate.

Left outer join is similar to Left join. This join shows matching record from both table and all the records from left table. The common records are determined by the one to many mapping column. As you can see in the below figure address_id is the mapping field for the two tables  : employee and address , which also act as join column or field for the two tables.

The project structure is given below :

The SQL query used to create the two database table is given below :

CREATE TABLE `employee` (
`employee_id` bigint(10) NOT NULL auto_increment, 
`firstname` varchar(50) default NULL, 
`lastname` varchar(50) default NULL, 
`cell_phone` varchar(15) default NULL, 
`address_id` bigint(20) default NULL, 
PRIMARY KEY (`employee_id`), 
KEY `FK_employee` (`address_id`), 
CONSTRAINT `FK_employee` FOREIGN KEY (`address_id`) REFERENCES `address` (`address_id`) 

CREATE TABLE `address` (
`address_id` bigint(20) NOT NULL auto_increment,
`street` varchar(50) default NULL,
`city` varchar(50) default NULL,
`state` varchar(50) default NULL,
`country` varchar(50) default NULL,
PRIMARY KEY (`address_id`)


hibernate.cfg.xml( \src\hibernate.cfg.xml )

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

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="">validate</property>

</hibernate-configuration> ( /src/net/roseindia/ )

package net.roseindia;

import java.util.Set;

public class Address {

private Long addressId;

private String street;

private String city;

private String state;

private String country;

private Set<Employee> employees;

public Long getAddressId() {
return addressId;

public void setAddressId(Long addressId) {
this.addressId = addressId;

public String getStreet() {
return street;

public void setStreet(String street) {
this.street = street;

public String getCity() {
return city;

public void setCity(String city) { = city;

public String getState() {
return state;

public void setState(String state) {
this.state = state;

public String getCountry() {
return country;

public void setCountry(String country) { = country;

public Set<Employee> getEmployees() {
return employees;

public void setEmployees(Set<Employee> employees) {
this.employees = employees;


Address.hbm.xml  ( /src/net/roseindia/Address.hbm.xml  )

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

<hibernate-mapping package="net.roseindia">
<class name="Address" table="address">
<id name="addressId" column="address_id">
<generator class="native" />
<property name="street" column="street"/>
<property name="city" column="city"/>
<property name="state" column="state"/>
<property name="country" column="country"/>
<set name="employees" table="employee"
inverse="true" lazy="true" fetch="select">
<column name="address_id" not-null="true" />
<one-to-many class="net.roseindia.Employee" />
</hibernate-mapping> ( /src/net/roseindia/ )

package net.roseindia;

public class Employee {
private Long employeeId;

private String firstname;

private String lastname;

private String cellphone;

private Address address;

public Address getAddress() {
return address;

public void setAddress(Address address) {
this.address = address;

public Employee() {}

public Employee(String firstname, String lastname, String phone) {
this.firstname = firstname;
this.lastname = lastname;
this.cellphone = phone;

public Long getEmployeeId() {
return employeeId;

public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;

public String getFirstname() {
return firstname;

public void setFirstname(String firstname) {
this.firstname = firstname;

public String getLastname() {
return lastname;

public void setLastname(String lastname) {
this.lastname = lastname;

public String getCellphone() {
return cellphone;

public void setCellphone(String cellphone) {
this.cellphone = cellphone;


Employee.hbm.xml(  \src\net\roseindia\Employee.hbm.xml  )

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

<hibernate-mapping package="net.roseindia">

<class name="Employee" table="employee">
<id name="employeeId" column="employee_id">
<generator class="native" />

<property name="firstname" column="firstname"/>
<property name="lastname" column="lastname" />
<property name="cellphone" column="cell_phone" />

<many-to-one name="address" class="net.roseindia.Address" fetch="select">
<column name="address_id" not-null="true" />

</hibernate-mapping> ( \src\net\roseindia\ )

package net.roseindia;

import java.util.*;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class LeftOuterJoin {
private static SessionFactory sf;
private static ServiceRegistry serviceRegistry;

public static void main(String[] args) {

try {
Configuration configuration = new Configuration().addResource(
.setProperty("hibernate.show_sql", "false");
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sf = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);

.println("*************************Hibernate Left Outer Join Example*************************");

Session session = sf.openSession();
String hql = "select e.firstname,e.lastname,e.cellphone,,a.state, from Employee e left join e.address as a";
Query query = session.createQuery(hql);

System.out.println("First Name\t" + "Last Name\t" + "Cell Phone\t"
+ "City\t\t" + "State\t" + "Country\t");

for (Iterator it = query.iterate(); it.hasNext();) {
Object[] row = (Object[]);
System.out.print("\t\t" + row[1]);
System.out.print("\t\t" + row[2]);
System.out.print("\t" + row[3]);
System.out.print("\t" + row[4]);
System.out.print("\t" + row[5]);



In the console, you will get the following output :

*************************Hibernate Left Join Example*************************
First Name  Last Name  Cell Phone  City      State   Country 
Romesh      Thapar      9999999999  New Delhi Delhi   India
Smita       Khanna      3333333333  New Delhi Delhi   India
Ranjan      Mathur      3332121213  null      null    null

Download Source Code



Posted on: March 6, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Left Outer Join  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image