开发者

Hibernate querying - issue with resolving property

Hey I have a small problem doing a query: the query line is: Query query = session.createQuery("from Recipe r where r.name='" + searchString + "' and r.ownerid=" + userId );

the .hbm.xml file is:

<?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="recipedifficul开发者_运维知识库ty" type="int">
   <column name="recipedifficulty" not-null="true" sql-type="INTEGER"/>
  </property>
 </class>
</hibernate-mapping>

I get an exception - saying "Could not resolve property ownerid of Recipe ..."

Why? What have I declared wrong? Or do I do the query in a wrong way?

Yoav


you either need to:

1) add ownerid property to the mapping

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

OR

2) use r.users.id in your query (assuming User's primary key property is 'id')

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

side-note 1: it's better using positional or named parameters in a query instead of string concatenation - http://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/

side-note 2: as long as 'users' association mapping is many-to-one then the more proper name for it is 'user' - there is only one user associated with each recipe

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜