This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Unable to fetch results using createQuery" Watch "Unable to fetch results using createQuery" New topic
Author

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;

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

Joined: Feb 05, 2001
Posts: 17258
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to fetch results using createQuery