Hibernate Subqueries

In this section, you will learn about the subqueries with an appropriate example.


 

Ads

Tutorials   
Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4.2.8 Maven dependency Hibernate 4.2.7 Maven dependency
Ads

Hibernate Subqueries

     

In this section, you will learn about the subqueries with an appropriate example.

Subqueries in hibernate can be referred as the queries that are surrounded by parentheses (). Subqueries solves the purpose of grouping, ordering , narrowing and aggregating the resultant of the query by using the where clause. Notice that the subqueries get executed prior to the main query. The HQL queries may contain the subqueries only in case of if the subqueries are supported by the underline database.

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"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

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

  <property name="name" type="string"/>
  <bag name="product" inverse="true" cascade="all,delete-orphan">
  <key column="did"/>
  <one-to-many class="net.roseindia.Product"/>
  </bag>

 </class>
</hibernate-mapping>

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""http:
//hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>

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

  <property name="name" type="string"/>
  <property name="did" type="int"/>
  <property name="price" type="double"/>
  <many-to-one name="dealer" class=
   "net.roseindia.Dealer" 
column="did" 
insert=
"false" update="false"/>

 </class>
 
</hibernate-mapping>

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

  /**
 @param args
 */
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  Session sess = null;
  try{
  SessionFactory fact = new 
   Configuration
().configure().buildSessionFactory();
  sess = fact.openSession();
  String sql_query = "select d.name,p.name,p.price 
   from Product p join p.dealer d where
    p.price in(select p.price from p where p.price > 1500)"
;
  Query query = sess.createQuery(sql_query);
  System.out.println("Dealer Name\t"+"Product Name\t"+"Price");
  for(Iterator it=query.iterate();it.hasNext();){
  Object[] row = (Object[]) it.next();
  System.out.print(row[0]);
  System.out.print("\t\t"+row[1]);
  System.out.print("\t"+row[2]);
  System.out.println();
  }
  sess.close();
  }
  catch(Exception e ){
  System.out.println(e.getMessage());
  }
  }

}

Output:

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

log4j:WARN Please initialize the log4j system properly.

Hibernate: select dealer1_.name as col_0_0_, product0_.name as col_1_0_, product0_.price as col_2_0_ from Product product0_ inner join Dealer dealer1_ on product0_.did=dealer1_.id where product0_.price in (select product0_.price from Product product0_ where product0_.price>1500)

Dealer Name  Product Name   Price

Agrawal   Computer  23000.0

Mohan   Mobile  15000.0

Ritu   HardDisk  2500.0

 

Advertisements

Share on Google+Share on Google+

Hibernate Subqueries

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

Advertisements

Ads
 

Discuss: Hibernate Subqueries   View All Comments

Post your Comment


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

Ads

 

Ads