2011-10-08 163 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: 只要“用户”关联映射是多到一个再更正确的名称是'用户' - 每个配方只有一个用户关联