hql语句错误
原语句select user from User where user.username = '" + username + "' ——>select user from Useruser where user.username = '" + username + "'
下面是别人的解析:
"from po.test where test.name = sdf"
sdf是个字符串值,应该加单引号。不然Hibernate会把它作为类的属性看待,而你的类中是没有sdf这个属性的
问题之二:
from后直接写类名,不必加包名
问题之三:
test是类名,不能直接使用test.name(不能通过类来访问,就好像你不能通过类名去访问实例变量一样),请不要问为什么,记住这是HQL的语法要求就行了。
那怎么办呢?通常会用别名的方式解决,比如
from test as t where t.name = 'sdf'(别名就好像是test类的一个对象,通过对象就可以访问实例变量啦,哈哈)
所以你也可以这样写:
from test as test where test.name = 'sdf'(注意现在test.name中的test是别名了)
当然,仅对一个类进行操作,也可以不借助于别名:
from test where name = 'sdf'