Share on Google+Share on Google+

Associations and Joins

This section includes a brief introduction about Associations and Joins along with examples.

Associations and Joins


This section includes a brief introduction about Associations and Joins along with examples.

Association mappings: Association mappings comes in the list of the most difficult thing to get right. This section includes the canonical cases one by one. Here we starts the section from unidirectional mappings and comes to the bi-directional cases.

We'll include the classification of associations that whether they map or not to an intervening join table and by multiplicity.

Here we are not using the Nullable foreign keys since these keys do not require good practice in traditional data modeling. Hibernate does not require the foreign keys since mappings work even if we drop the nullability constraints.

Hibernate HQL Inner Join

Hibernate's HQL language is not capable for handling the "inner join on" clauses. If our domain entity model  includes relationships defined between the related objects then the query like this 

Query query = session.createQuery("from Car car inner join Owner owner where owner.Name ='Vinod'");

will work. HQL keeps the information of joining the Car and Owner classes according to the association mapping contained in the hbm.xml file. Since the association information is defined in the mapping file so there is no need to stipulate the join in the query. In the above query "from Car car where car.Owner.Name='Vinod'" will work too. Initialization of the collections and many-to-one mapping requires the explicit joins just to avoid lazy load errors.

A unidirectional one-to-many association on a foreign key is rarely required.

<?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE hibernate-mapping
 PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"

 <class name="net.roseindia.Dealer">
  <id name="id" type="int">
 <generator class="increment"/>

  <property name="name" type="string"/>
  <bag name="product" inverse="
  <key column="did"/>
  <one-to-many class="


A unidirectional one-to-many association on a foreign key is rarely required.

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

 <class name="net.roseindia.Product">
  <id name="id" type="int">
 <generator class="increment"/>

  <property name="name" type="string"/>
  <property name="did" type="int"/>
  <property name="price" type="double"/>
  <many-to-one name="dealer" class="
"false" update="false"/>


Here is the hibernate code:

In this example first we create the session object with the help of the SessionFactory interface. Then we use the createQuery() method of the Session object which returns a Query object. Now we use the openSession() method of the SessionFactory interface simply to instantiate the Session object. And the we retrieve the data from the database store it in that Query object and iterate this object with the help of Iterator and finally displays the requested data on the console.

package net.roseindia;

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 Join {

 @param args
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  Session sess = null;
  SessionFactory fact = new 
  sess = fact.openSession();
  String sql_query = "from 
Product p inner join as d"
  Query query = sess.createQuery(sql_query);
  Iterator ite = query.list().iterator();
+"Product Name\t"+"Price");
  while ite.hasNext() ) {
  Object[] pair = (Object[]);
  Product pro = (Productpair[0];
  Dealer dea = (Dealerpair[1];
  catch(Exception e ){




log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select as id1_0_, as id0_1_, as name1_0_, product0_.did as did1_0_, product0_.price as price1_0_, as name0_1_ from Product product0_ inner join Dealer dealer1_ on

Dealer Name  Product Name  Price

Computer  Agrawal   23000.0

Keyboard  Agrawal   1500.0

Computer  Agrawal  100.0

Mobile  Mohan   15000.0

PenDrive   Mohan   200.0

Laptop  Ritu  200.0

HardDisk   Ritu   2500.0



Posted on: March 10, 2008 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: Associations and Joins   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 2, 2011
Table definitions

Can you please provide the table defintions for Dealer and Product.
December 9, 2011
Class definitions are missing

Product and Dealer class definitions are missing
Deepak Shinde
May 18, 2012
hibernate.cfg.xml not found

Hi I am tring to execute simple hibernate example while compling the application i am getting this error 'hibernate.cfg.xml file not found' i have checked whole application didnt find anything wrong please guide me through this.