Hibernate Select Clause

In this lesson we will write example code to select the data from Insurance table using Hibernate Select Clause.


 

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 Select Clause

     

In this lesson we will write example code to select the data from Insurance table using Hibernate Select Clause. The select clause picks up objects and properties to return in the query result set. Here is the query:

Select insurance.lngInsuranceId, insurance.insuranceName, insurance.investementAmount, insurance.investementDate from Insurance insurance

which selects all the rows (insurance.lngInsuranceId, insurance.insuranceName, insurance.investementAmount, insurance.investementDate) from Insurance table.

Hibernate generates the necessary sql query and selects all the records from Insurance table. Here is the code of our java file which shows how select HQL can be used:


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 Select Clause Example
 */
public class SelectClauseExample {
  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();
 
  //Create Select Clause HQL
 String SQL_QUERY ="Select insurance.

lngInsuranceId,insurance.insuranceName," 
 "insurance.investementAmount,insurance.

investementDate from Insurance insurance";
 Query 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("Amount: " + row[2]);
 }
 
  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:

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_

ID: 1

Name: Car Insurance

Amount: 1000

ID: 2

Name: Life Insurance

Amount: 100

ID: 3

Name: Life Insurance

Amount: 500

ID: 4

Name: Car Insurance

Amount: 2500

ID: 5

Name: Dental Insurance

Amount: 500

ID: 6

Name: Life Insurance

Amount: 900

ID: 7

Name: Travel Insurance

Amount: 2000

ID: 8

Name: Travel Insurance

Amount: 600

ID: 9

Name: Medical Insurance

Amount: 700

ID: 10

Name: Medical Insurance

Amount: 900

ID: 11

Name: Home Insurance

Amount: 800

ID: 12

Name: Home Insurance

Amount: 750

ID: 13

Name: Motorcycle Insurance

Amount: 900

ID: 14

Name: Motorcycle Insurance

Amount: 780

Advertisements

Share on Google+Share on Google+

Hibernate Select Clause

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

Advertisements

 

Discuss: Hibernate Select Clause   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
jayesh patil
November 30, 2011
table and class name

You have given a sql query "Select insurance.lngInsuranceId, insurance.insuranceName, insurance.investementAmount, insurance.investementDate from Insurance insurance" In this the last Insurance insurance what does it signify? Is it Class name and Table name respctively??
S.Raghavendra
February 9, 2012
about hql Query

in hql query when i am iterating using the iterator it is saying that cannot convert int to int to Insurance.I have tried a lot but it is not comming.Here is the code of yours only.I am tring your example only in myeclipse....reply me soon String Sql_Query="Select insurance.id,insurance.insurance_name," + "insurance.investement_amount,insurance.investment_date from Insurance insurance"; Query query=hsession.createQuery(Sql_Query); for(Iterator it=(Iterator)query.iterate();it.hasNext();) { Cannot convert int to Insurance(error) -> Insurance insurance=(Insurance)it.next(); System.out.println("ID"+insurance.getId()); System.out.println("Name"+insurance.getInsuranceName()); }
Ramakrishna Brahma
August 30, 2012
class cast exception occurs

Object[] row=(Object[]) it.next(); this line class cast exception occurs how to solve it
sontos
September 13, 2012
hibernate query

very good---------------
Chandrashekhar
November 3, 2012
SessionFactory should be singleton

In case of web applications, the SessionFactory should be accessed in a singleton way, it can cause out-of-memory-error otherwise. Since the above program is a demo example, standalone and run once, it is fine. Take Care of this in real projects.

Ads

 

Ads