File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes about Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "about Hibernate" Watch "about Hibernate" New topic
Author

about Hibernate

George Ren
Ranch Hand

Joined: Aug 17, 2002
Posts: 58
Hi,everyone!
I use HQL to query the database in my program. Here is my code:



I have looked many source code about querying in hibernate, and I think my HQL query is nothing wrong. But when running, there is always

net.sf.hibernate.QueryException: unexpected token: as [select from userinfo as ui]

I am really confused. Please give me your advices if possible.

Thanks for help!

Have a nice day!

Regards,


THINK!
George Ren
Ranch Hand

Joined: Aug 17, 2002
Posts: 58
Sorry, actually my code is:
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
So what's the exception you get with this new code?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
George Ren
Ranch Hand

Joined: Aug 17, 2002
Posts: 58
the same exception. A paste the wrong code first time, the last code is actually the code I run, and got the exceptions above.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
The same exception? Even though your code doesn't use "select from userinfo as ui" in any query?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

That error means you've an unrecognised word in your HQL. This could be because you've mapped userinfo wrong - check the mapping file, remeber it is case sensetive.

Also - you probably could do with getting rid of the nested try/catch blocks - they are unecessary, and make the code less clear.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
George Ren
Ranch Hand

Joined: Aug 17, 2002
Posts: 58
I think there's nothing wrong with the mapping. The table userinfo in database is created in program using Hibernate also. And below is my UserInfo class and the UserInfo.hbm.xml file:


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Remember that with Hibernate you are qureying in HQL unless you specifically specify SQL. So:

is looking for all userinfo Objects in a Hibernate Session, not for the contents of the userinfo table. Are there any objects in the Session with that name? Well unless you've defined it elsewhere, then no. But there is a UserInfo.
George Ren
Ranch Hand

Joined: Aug 17, 2002
Posts: 58
Thanks you to give me advises ,Lasse Paul. And finally I know what is wrong. I give the mapping name uncorrently.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about Hibernate