Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Hibernate
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

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

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

8 comments so far (post your own) View All Comments Latest 10 Comments:

Have you tried using antlr-2.7.6.jar in the classpath...
This will help, as i was having same exception, Now it's working fine..

-Saket

Posted by Saket on Friday, 12.14.07 @ 17:31pm | #42261

You can copy antlr-2.7.6.jar in your classpath.
This will help, as i was getting the same, but now..it's working fine..

Posted by Saket on Friday, 12.14.07 @ 17:28pm | #42260

u have to diaplay the java

Posted by k.v.s.s.Prasad on Thursday, 07.12.07 @ 14:44pm | #21138

I am experiencing a prob while tryign to execute a select query. It throws "Object not mapped" error in createQuery() statement. I have the mapping xml & appropriate java object...

Following is the code:

String qryString =
" FROM USERS AS USR " +
" WHERE USR.LOGIN_NAME = '" + userData.getLoginName() + "'" +
" AND USR.PASSWORD = '" + userData.getPassword() + "'";

Query hbnQry = session.createQuery(qryString);

Please help.

Posted by ganesh on Wednesday, 06.20.07 @ 17:29pm | #19789

Exception in thread "main" java.lang.NoClassDefFoundError: antlr/ANTLRException
at org.hibernate.hql.ast.ASTQueryTranslatorFactory.createQueryTranslator(ASTQueryTranslatorFactory.java:35)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:72)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
at hiber.SelectHQLExample.main(SelectHQLExample.java:29)
Java Result: 1
while running this example

Posted by soumita roy on Tuesday, 05.22.07 @ 16:08pm | #17007

how doi install hybernate without a database.is it possible to work local by adding jars to the classpath

Posted by bhavesh thanki on Monday, 02.19.07 @ 21:02pm | #8400

very awesome tutorial for beginners.but they have to be precise on what saying

Posted by NewEncodings on Tuesday, 02.13.07 @ 17:42pm | #7761

Deepak there is no need for creating preparestatement isn't it?

Posted by janardhan on Tuesday, 12.12.06 @ 12:01pm | #880

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.