Thursday , May 25 2017
Home / Hibernate / Hibernate Right Join Example

Hibernate Right Join Example

In this tutorial, we are going to show you how to work with Hibernate Right Join.

What is Right Join ?

Right Join is a keyword in SQL. Which returns all the data from right hand side (table2) and matched records from left hand side table (table1). We can see the NULL values from the left side if there is no match.

Hibernate Right 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, those 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 child table’s foreign key.

Hibernate Right Join Example :

In the previous tutorials, we have discussed about Hibernate Left Join, here I am taking the same example for Right Joibles like Customer and Items these two are having one to many relationship. That means one Customer can have multiple Items.

Project Structure :

Hibernate Right Join Example

Create customer and item tables with data to apply right join.

Customer Table :

Hibernate Right Join Customer

Item Table :

Hibernate Right Join Item

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;


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;



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "">
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/onlinetutorialspoint</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="show_sql">true</property>
        <mapping class="com.otp.hibernate.pojo.Customer" />
        <mapping class="com.otp.hibernate.pojo.Item" />

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");


Output :

Hibernate Right Join Output

Happy Learning 🙂

Download Example

About chandrashekhar

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+ ,

Check Also

Hibernate Restrictions with Example

In this tutorials, we are going to learn about Hibernate Restrictions with Example. In the …

Leave a Reply

Your email address will not be published. Required fields are marked *