Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

HQL Where Clause Example

Where Clause is used to limit the results returned from database.

Ads

Tutorials   
When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? 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
Ads

HQL Where Clause Example

     

Where Clause is used to limit the results returned from database. It can be used with aliases and if the aliases are not present in the Query, the properties can be referred by name. For example:

from Insurance where lngInsuranceId='1'

Where Clause can be used with or without Select Clause. Here the example code:

package roseindia.tutorial.hibernate;

import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;

import java.util.*;

/**
 @author Deepak Kumar
 *
 * http://www.roseindia.net
 * HQL Where Clause Example
 * Where Clause With Select Clause Example
 */
public class WhereClauseExample {
  public static void main(String[] args) {
  Session session = null;

  try{
  // This step will read hibernate.cfg.
xml and prepare hibernate for use

  SessionFactory sessionFactory = new 
Configuration().configure().
buildSessionFactory
();
  session =sessionFactory.openSession();
 
  System.out.println("***************
****************"
);
  System.out.println("Query using 
Hibernate Query Language"
);
  //Query using Hibernate Query Language
 String SQL_QUERY =" from Insurance
 as insurance where insurance.
lngInsuranceId='1'"
;
 Query query = session.createQuery
(SQL_QUERY);
 for(Iterator it=query.iterate()
;it.hasNext();){
 Insurance insurance=(Insurance)it
.next
();
 System.out.println("ID: " + insurance.
getLngInsuranceId
());
 System.out.println("Name: " 
+ insurance. getInsuranceName());
 
 }
 System.out.println("****************
***************"
);
 System.out.println("Where Clause With
 Select Clause"
);
  //Where Clause With Select Clause
 SQL_QUERY ="Select insurance.
lngInsuranceId,insurance.insuranceName," 
+
 "insurance.investementAmount,
insurance.investementDate from Insurance
 insurance "
" where insurance.
lngInsuranceId='1'"
;
 query = session.createQuery(SQL_QUERY);
 for(Iterator it=query.iterate();it.
hasNext
();){
 Object[] row = (Object[]) it.next();
 System.out.println("ID: " + row[0]);
 System.out.println("Name: " + row[1]);
 
 }
 System.out.println("***************
****************"
);

  session.close();
  }catch(Exception e){
  System.out.println(e.getMessage());
  }finally{
  }  
  }
}

To run the example select Run-> Run As -> Java Application from the menu bar. Following out is displayed in the Eclipse console:

*******************************

Query using Hibernate Query Language

Hibernate: select insurance0_.ID as col_0_0_ from insurance insurance0_ where (insurance0_.ID='1')

ID: 1

Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?

Name: Car Insurance

*******************************

Where Clause With Select Clause

Hibernate: select insurance0_.ID as col_0_0_, insurance0_.insurance_name as col_1_0_, insurance0_.invested_amount as col_2_0_, insurance0_.investement_date as col_3_0_ from insurance insurance0_ where (insurance0_.ID='1')

ID: 1

Name: Car Insurance

*******************************

Advertisements

Share on Google+Share on Google+

HQL Where Clause Example

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

Advertisements

Ads
 

Discuss: HQL Where Clause Example   View All Comments

Post your Comment


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

Ads

 

Ads