---
title: "Hibernate别名问题"
author: "青蜂侠"
date: "2009-12-06T16:17:34Z"
tags: []
slug: "hibernate"
draft: false
lastmod: "2009-12-06T16:59:20Z"
id: 542733
url: "https://brucefeng.iteye.com/blog/542733"
---

有个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";  
```

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