Hibernate别名问题

有个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" unsaved-value="null">
  		<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>
 

 在查询的时候使用下列语句

Sql代码
 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]

 

当将hql语句修改为

String hql = "from BookComment where userpk=:author order by title asc  
 

后可以顺利查询出结果

 

其实分析hql代码可以发现查询条件中的userpk是数据库中的列名,而Hibernate是ORM框架,其重要作用就是以对象的方式操作数据库。这样修改hql代码如下:

String hql = "from BookComment as bc where bc.author=:author order by bc.title asc";  

 即可顺利得到查询结果,这也是更符合面向对象的思想。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值