Hibernate Subquery

In this section we will discuss about hibernate Subquery.

Ads

Tutorials   
Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example 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
Ads

Hibernate Subquery

In this section we will discuss about hibernate Subquery.

Hibernate Subquery:

Hibernate Subqueries are surrounded by parentheses ().Hibernate supports subqueries within queries for database that support subselect.
By writing subqueries you can group, order and aggregate your resultant of query by using where clause /in clause.
Subqueries executed before the query run.
You can write subquery by using native SQL ,HQL or Criteria.

Example : In this example we are writing subquery to find out employee whose id is equal to 3 or greater than 5.

Here is main class code -

package net.roseindia.main;

import java.util.*;

import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class MainClass {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession(); // session is opened

String hql = "SELECT emp.id, emp.name, emp.salary, emp.dateOfJoin from Employee emp " +
"WHERE emp.id IN (SELECT emp.id FROM emp Employee WHERE emp.id=3 OR emp.id>5) group by emp.id";
Query query = session.createQuery(hql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

List objectList = query.list();
Iterator iterator = objectList.iterator();
System.out.println("Emp Id\tEmployee Name\tSalary\tDate of Join");
while (iterator.hasNext()) {
Map map = (Map) iterator.next();
System.out.print(map.get("0"));
System.out.print("\t" + map.get("1"));
System.out.print("\t" + map.get("2"));
System.out.print("\t" + map.get("3"));
System.out.println();
}

session.close(); // session is closed
}
}

Output:

Hibernate: select employee0_.emp_id as col_0_0_, employee0_.name as col_1_0_, employee0_.salary as col_2_0_, employee0_.date_of_join as col_3_0_ from employee employee0_ where employee0_.emp_id in (select employee0_.emp_id from employee employee0_ where employee0_.emp_id=3 or employee0_.emp_id>5) group by employee0_.emp_id
Emp Id   Employee Name    Salary      Date of Join
3        Mac              15000       2003-12-23 00:00:00.0
6        Linda            20000       2010-06-12 00:00:00.0

Download complete source code

Advertisements

Ads
Share on Google+Share on Google+

Hibernate Subquery

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

Advertisements

 

Discuss: Hibernate Subquery  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Maira
August 23, 2012
Very weel

I loved your post... Tranks!

Ads

 

Ads