Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to fetch results using createQuery

sreenivas jeenor
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic