-->
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.  [ 5 posts ] 
Author Message
 Post subject: CreateQuery vs CreateSQLQuery
PostPosted: Sun Aug 22, 2010 8:25 pm 
Newbie

Joined: Sun Apr 18, 2010 1:08 am
Posts: 6
Hi all,
New to hibernate and have a question.

The following works just fine;
List Users = session.CreateQuery("from users").list();

Iterator i = Users.iterator();
while (i.hasNext()){
Users usr = (Users) i.next();
System.out.println(usr.getName());
}

but when i try
List Users = session.CreateSQLQuery("SELECT * FROM Users").list();
I get an error message here:
Users usr = (Users) i.next(); <--- complained about casting object to User!!

Thanks,


Top
 Profile  
 
 Post subject: Re: CreateQuery vs CreateSQLQuery
PostPosted: Mon Aug 23, 2010 1:28 am 
Newbie

Joined: Tue Aug 17, 2010 3:29 pm
Posts: 12
When you are using
Code:
CreateSQLQuery
you have to pass native sql query. you cannot pass HQL query.


Top
 Profile  
 
 Post subject: Re: CreateQuery vs CreateSQLQuery
PostPosted: Mon Aug 23, 2010 1:55 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There is an entire chapter in the documentation about native queries in the documentation which, among other things, will tell you how to return entities: http://docs.jboss.org/hibernate/stable/ ... rysql.html


Top
 Profile  
 
 Post subject: Re: CreateQuery vs CreateSQLQuery
PostPosted: Mon Aug 23, 2010 3:46 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 8:18 am
Posts: 35
in the query you have to add the entity.
List Users = session.CreateSQLQuery("SELECT * FROM Users").addEntity(Users.class).list();


Top
 Profile  
 
 Post subject: Re: CreateQuery vs CreateSQLQuery
PostPosted: Mon Aug 23, 2010 8:01 am 
Newbie

Joined: Sun Apr 18, 2010 1:08 am
Posts: 6
yaragalla_murali wrote:
When you are using
Code:
CreateSQLQuery
you have to pass native sql query. you cannot pass HQL query.


yup i did that, i forgot to write the correct form here in fourm, thanks.

nordborg wrote:
There is an entire chapter in the documentation about native queries in the documentation which, among other things, will tell you how to return entities: http://docs.jboss.org/hibernate/stable/ ... rysql.html


I looked at it before i posted but will check it out again, thanks.

achyut wrote:
in the query you have to add the entity.
List Users = session.CreateSQLQuery("SELECT * FROM Users").addEntity(Users.class).list();


Thanks, i will give this a try.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.