很奇怪的问题org.hibernate.QueryException: Unable to resolve path

kedakeda 2008-10-16 07:52:14
今天搞了个测试用myeclipse下oracle8.1.7和hibernate3.0+spring2.0做个测试,平台是tomcate6.0

建了一个表叫test,里面有name,age等等几个属性吧
然后做映射
然后写dao查询
用了HibernateTemplate
然后就出现怪事了
如果用回调,使用createSQLQuery来运行sql语句,就没问题,一切查询正常

如果运行hql语句,比如"from po.test where test.name = sdf"
不管是直接用HibernateTemplate还是用回调,都会报下面这个错

org.hibernate.QueryException: Unable to resolve path [test.name], unexpected token [test] [SELECT * from po.test where test.name = sdf]


配置文件test.hbm.xml,把头部声明去掉了,

<hibernate-mapping>
<class name="po.test" table="TEST" schema="TEST">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="sequence" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="12" />
</property>
………………此处省略许多property配置
</class>
<hibernate-mapping>




applicationContext.xml也省略了头尾,拷来一个SessionFactory和一个dao的设置部分

<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="DataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle8iDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>po/test.hbm.xml</value>

</list>
</property>
</bean>
<bean id="testDAO" class="dao.testDAO">
<property name="sessionFactory">
<ref bean="SessionFactory" />
</property>
</bean>



请问到底是什么问题呢?
...全文
4957 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gongli109 2012-10-14
  • 打赏
  • 举报
回复
7楼讲的真好!!
qq251807785 2011-03-23
  • 打赏
  • 举报
回复
使用别名
kedakeda 2008-10-17
  • 打赏
  • 举报
回复
用了4楼的办法
解决了

但是是什么原因呢?
期望大家解惑
ri881226 2008-10-17
  • 打赏
  • 举报
回复
问题应该解决了吧?!!~
kedakeda 2008-10-17
  • 打赏
  • 举报
回复
多谢楼上的

了解了
accp206 2008-10-17
  • 打赏
  • 举报
回复
就从你的这句HQL来分析原因吧:
"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'
accp206 2008-10-16
  • 打赏
  • 举报
回复
楼主试试这个:
from test as t where t.name = 'sdf'
kedakeda 2008-10-16
  • 打赏
  • 举报
回复
不用po.test
改成test也是一样的
zhj92lxs 2008-10-16
  • 打赏
  • 举报
回复
from test a where a.name = sdf 试试
老紫竹 2008-10-16
  • 打赏
  • 举报
回复
from po.test
为啥要写po呢?难道你的类不叫test吗?
不用写package的

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧