File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Unable to fetch results using createQuery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Unable to fetch results using createQuery" Watch "Unable to fetch results using createQuery" New topic

Unable to fetch results using createQuery

sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125
Hi All.

In my Code i am tyring tring fetch values based on condition.

Here the code
import org.hibernate.*;
import org.hibernate.Session;
import org.hibernate.cfg.*;

import java.util.*;

public class SelectHQLExample {

public static void main(String[] args) {
Session session = null;

// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();

String searchString="Car";
String sql="From Insurance p where p.lngInsuranceId :id";
String queryString ="from Insurance p where p.insuranceName like ?";
Query q = session.createQuery(queryString).setString(0,searchString);

for(Iterator it=q.iterate();it.hasNext() {
Object[] row = (Object[]);
System.out.println("ID: " + row[0]);


}catch(Exception e){


The output what i am getting is

Hibernate: select insurance0_.ID as col_0_0_ from insurance insurance0_ where in
surance0_.insurance_name like ?

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

You are missing the call that runs the query.

q.list() this returns a List of results, but you just create the Query object and try to call q.iterate(), is there even such a method? Anyway, instead of calling q.iterate call

List l = q.list(); and look a the values in l.

Also remove that String sql, because it is not valid HQL in the Where portion of the query.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Unable to fetch results using createQuery
It's not a secret anymore!