-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Error when use simple query !!!
PostPosted: Fri Jun 19, 2009 7:43 pm 
Newbie

Joined: Fri Jun 19, 2009 7:02 pm
Posts: 4
I wite a application, my application is insert, delete, update good, but i can't query by "select * from ...", it error here
Code:
Query quey = session.createQuery("select * from Person");


this is my class Main
Code:
import java.util.Iterator;
import java.util.List;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class Main {
    public static void main(String[] args) {
        SessionFactory sessions = new Configuration().configure().buildSessionFactory();
        Session session = sessions.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
             //insert, update, delete is good here
            Query query = session.createQuery("select * from PERSON");
            List list = query.list();
            Iterator iter = list.iterator();
            int i = 0;
            while ( iter.hasNext() ){
                i++;
                Person p = (Person)iter.next();
                System.out.println("Person " + i + " " + p.getName());
            }
            tx.commit();
            tx = null;
        } catch ( HibernateException e ) {
            if ( tx != null ) tx.rollback();
            e.printStackTrace();
        } finally {
            session.close(); 
        }           
    }
}


It is insert, update, delete good but can't execute query !! Help me plz, Thank a lot of


Top
 Profile  
 
 Post subject: Re: Error when use simple query !!!
PostPosted: Sat Jun 20, 2009 3:59 am 
Newbie

Joined: Sat Jun 20, 2009 3:48 am
Posts: 2
I guess you have to pass a HQL strng to
session.createQuery , like in your program, I think it is "from Person",
only not "select *".

Please let me knw if it works this way.

Thanks,
Ishtiak
http://www.interview-questions-tips-forum.net


Top
 Profile  
 
 Post subject: Re: Error when use simple query !!!
PostPosted: Sat Jun 20, 2009 5:22 am 
Newbie

Joined: Fri Jun 19, 2009 7:02 pm
Posts: 4
Thank, it is "from Person" !!!

Can you tell me how to select some column in table

it same : "select cname, iage from Person" !! Can it do ??


Top
 Profile  
 
 Post subject: Re: Error when use simple query !!!
PostPosted: Sat Jun 20, 2009 8:17 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
Example for getting specific column using query.

Session session = factory.openSession();
String query = " select e.name, e.startDate from Event e ";
Query query = session.createQuery("query");
List results = query.list();
for (Iterator I = results.iterator(); i.hasNext();) {
Object[] row = (Object[]) i.next();
String name = (String) row[0];
Date startDate = (Date) row[1];
// ...
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.