有个BookComment类的映射文件如下
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.brucefeng.sinter.domain"> <class name="BookComment" table="comment"> <id name="bookCommentPK" column="commentpk"> <generator class="uuid.hex"></generator> </id> <property name="title" type="string" column="title"></property> <property name="content" type="text" column="content"></property> <many-to-one name="author" column="userpk" class="org.brucefeng.sinter.domain.User" cascade="save-update" fetch="join" lazy="false"></many-to-one> </class> </hibernate-mapping>
在查询的时候使用下列语句
String hql = "from BookComment as bc where bc.userpk=:author order by bc.title asc";
执行代码时出现如下问题:
org.hibernate.QueryException: could not resolve property: userpk of: org.brucefeng.sinter.domain.BookComment [from org.brucefeng.sinter.domain.BookComment as bc where bc.userpk=:author order by bc.title asc]
谢谢。
String hql = "from BookComment where userpk=:author order by title asc
[code="xml"]
class="org.brucefeng.sinter.domain.User"
cascade="save-update" fetch="join" lazy="false">
[/code]
[quote] name="author" [/quote]
所以应该用[color=red]bc.author[/color]而不是[color=red]bc.userpk[/color]
应该不是别名的问题,author 是另外一个表中的字段,并不在当前表中。
要么就是userpk是一个实体类中的对象,不能直接点出对象的,要点到他的下一级属性
[code="java"]
String hql = "from BookComment as bc where bc.author =:author order by bc.title asc";
[/code]
这样才对。hql应该使用xml里配置的name而不是column。其它都对。