Win a copy of Mesos in Action this week in the Cloud/Virtualizaton 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;

try{
// 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[]) it.next();
System.out.println("ID: " + row[0]);

}



session.close();
}catch(Exception e){
System.out.println("vasu........."+e.getMessage());
}finally{
}

}
}

-------------------------------
The output what i am getting is
----------------------------

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

Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
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.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic