2011-10-08 115 views
0

嘿我有一個小問題做一個查詢: 查詢行是: 查詢query = session.createQuery(「從配方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> 

我得到一個異常 - 他說: 「無法解析財產OWNERID方......」

爲什麼? 我宣佈錯了什麼?或者我以錯誤的方式進行查詢?

約阿夫

回答

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: 只要「用戶」關聯映射是多到一個再更正確的名稱是'用戶' - 每個配方只有一個用戶關聯