Hibernate查询 - 解决属性的问题

时间:2011-10-08 12:39:58

标签: hibernate

嘿,我在查询时遇到一个小问题: 查询行是: 查询query = session.createQuery(“来自Recipe r,其中r.name ='”+ searchString +“'和r.ownerid =”+ userId);

.hbm.xml文件是:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 26, 2011 10:56:44 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
 <class catalog="icdb" name="com.icdb.data.Recipe" table="recipes">
  <id name="recipeid" type="int">
   <column name="recipeid"/>
   <generator class="identity"/>
  </id>
  <many-to-one class="com.icdb.data.User" fetch="select" name="users">
   <column name="ownerid" not-null="true"/>
  </many-to-one>
  <property generated="never" lazy="false" name="releasedate" type="timestamp">
   <column length="19" name="releasedate" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="preparationtime" type="int">
   <column name="preparationtime" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="name" type="string">
   <column length="50" name="name" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="description" type="string">
   <column length="200" name="description"/>
  </property>
  <property generated="never" lazy="false" name="lastupdated" type="timestamp">
   <column length="19" name="lastupdated" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="servecount" type="java.lang.Integer">
   <column name="servecount"/>
  </property>
  <property generated="never" lazy="false" name="complete" type="boolean">
   <column name="complete" not-null="true"/>
  </property>
  <set fetch="select" inverse="true" lazy="true" name="recipepictureses"
   sort="unsorted" table="recipepictures">
   <key>
    <column name="recipeid" not-null="true"/>
   </key>
   <one-to-many class="com.icdb.data.RecipePicture"/>
  </set>
  <set fetch="select" inverse="true" lazy="true"
   name="recipeingredientses" sort="unsorted" table="recipeingredients">
   <key>
    <column name="recipeid" not-null="true"/>
   </key>
   <one-to-many class="com.icdb.data.RecipeIngredient"/>
  </set>
  <set fetch="select" inverse="true" lazy="true" name="recipetipses"
   sort="unsorted" table="recipetips">
   <key>
    <column name="recipeid" not-null="true"/>
   </key>
   <one-to-many class="com.icdb.data.RecipeTip"/>
  </set>
  <set fetch="select" inverse="true" lazy="true" name="recipereviewses"
   sort="unsorted" table="recipereviews">
   <key>
    <column name="recipeid" not-null="true"/>
   </key>
   <one-to-many class="com.icdb.data.RecipeReview"/>
  </set>
  <set fetch="select" inverse="true" lazy="true"
   name="recipeinstructionses" sort="unsorted" table="recipeinstructions">
   <key>
    <column name="recipeid" not-null="true"/>
   </key>
   <one-to-many class="com.icdb.data.RecipeInstruction"/>
  </set>
  <property name="recipedifficulty" type="int">
   <column name="recipedifficulty" not-null="true" sql-type="INTEGER"/>
  </property>
 </class>
</hibernate-mapping>

我得到一个例外 - 说“无法解析食谱的属性所有者......”

为什么呢? 我说错了什么?或者我是否以错误的方式进行查询?

约阿夫

1 个答案:

答案 0 :(得分:2)

你要么:

1)将ownerid属性添加到映射

<property name="ownerid" type="int">
   <column name="ownerid" not-null="true"/>
</property>

OR

2)在查询中使用r.users.id(假设用户的主键属性为'id')

Query query = session.createQuery("from Recipe r where r.name='" + searchString + "' and r.users.id=" + userId );

旁注1: 最好在查询中使用位置或命名参数而不是字符串连接 - http://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/

旁注2: 只要'用户'关联映射是多对一的,那么它的更合适的名称就是'用户' - 每个食谱只有一个用户