Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate QuerySyntaxException with MySql

 
Shadi Mehrabadi
Greenhorn
Posts: 12
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm using hibernate to connect MySQL DB, I made a persistence-unit like below and added my class in it:



in my config file, I made an entity manager factory bean and added the data source in it:



The person class in an entity and a table.

now... when I wanna run this Query, I see an exception:



the exception is:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [SELECT * FROM com.example.domain.Person P WHERE P.userName = UserName]




my Query is right.. why I see this exception?!

I changed my Query to this:



then I saw this:

org.hibernate.QueryException: could not resolve property: FirstName of: com.example.domain.Person [SELECT P.userName, P.password, P.FirstName, P.LastName FROM com.example.domain.Person P WHERE P.userName = UserName]



my domain class is:



what should I do?! please help me
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Selecting all properties
You cannot use the * operator in HQL (you are using HQL here, not SQL). Instead of

Use

or


Query parameters
Instead of simply using "Username" in your query, you need to use ":Username"
 
Shadi Mehrabadi
Greenhorn
Posts: 12
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh... Thanks James

I'm on it for two days,

when I read your post, I found this for the HQL,

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html


I didn't know it's name

thanks man,
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shadi

Be sure to use the reference guide that matches your version of Hibernate. That one is for 3.3.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic