JavaBeat
Hibernate Home Articles Resources Tutorials Hibernate Interview Questions & FAQs JSF QnA Code Junction

1. What is Hibernate?

2. Hibernate Architecture

3. Writing First Hibernate Code

4. Understanding Hibernate O/R Mapping

5. Understanding Hibernate <generator> element

6. Using Hibernate <generator> to generate id incrementally

7. Hibernate Query Language

8. Preparing table for HQL Examples

9. Writing ORM for Insurance table

10. HQL from clause Example

11. Hibernate Select Clause

12. HQL Where Clause Example

13. HQL Group By Clause Example

14. HQL Order By Example

15. Hibernate Criteria Query Example

16. Criteria Query Examples

17. Hibernate Native SQL Example

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:



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

import java.util.*;

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

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




Sponsors
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Latest QnA
Describe the lifecycle of a receiver application in order to receive a message?
Messages are not successful until they have been acknowledged. What are the types of acknowledgments?
What happens to messages if a transaction is rolled back?
What is the Role of the JMS Provider?
What is JMS administered object ?

JavaBeat Media (2004-2008), India
javabeat | planetoss | links directory | advertise
Copyright (2004 - 2008), JavaBeat