This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about Hibernate

 
George Ren
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
George Ren
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, actually my code is:
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what's the exception you get with this new code?
 
George Ren
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same exception? Even though your code doesn't use "select from userinfo as ui" in any query?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
George Ren
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you to give me advises ,Lasse Paul. And finally I know what is wrong. I give the mapping name uncorrently.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic