2012-04-09 59 views
0

我在數據庫用戶和鏈接中有2個表。用戶標識在鏈接表中是外來的。我已經使用hibernate reverse engg xml在java中創建模型類。 它創建了用戶,鏈接,LinkId類。這裏Link類鏈接2個表,而LinkId包含Link的屬性。 我正在嘗試使用用戶標識查詢鏈接表。我的查詢是「createQuery( 」from com.paypal.socialpay.models.LinkId li where li.userid =?「)。setInteger(0,id).list();」寫入查詢外鍵關聯

但在執行查詢時,我得到「java.lang.IllegalArgumentException:查詢中沒有位置參數:來自com.paypal.socialpay.models.LinkId li where li.userid =?」

有人能告訴我什麼,我做錯了

class name="com.paypal.socialpay.models.Link" table="link" catalog="socialdb"> 
    <composite-id name="id" class="com.paypal.socialpay.models.LinkId"> 
     <key-property name="id" type="int"> 
      <column name="id" /> 
     </key-property> 
     <key-property name="userid" type="java.lang.Integer"> 
      <column name="userid" /> 
     </key-property> 
     <key-property name="title" type="string"> 
      <column name="title" length="100" /> 
     </key-property> 
     <key-property name="price" type="string"> 
      <column name="price" length="100" /> 
     </key-property> 
     <key-property name="description" type="string"> 
      <column name="description" length="500" /> 
     </key-property> 
     <key-property name="contentname" type="string"> 
      <column name="contentname" length="100" /> 
     </key-property> 
     <key-property name="contentpreviewname" type="string"> 
      <column name="contentpreviewname" length="100" /> 
     </key-property> 
     <key-property name="contentdisplayname" type="string"> 
      <column name="contentdisplayname" length="100" /> 
     </key-property> 
     <key-property name="contentpreviewdisplayname" type="string"> 
      <column name="contentpreviewdisplayname" length="100" /> 
     </key-property> 
     <key-property name="downloadlink" type="string"> 
      <column name="downloadlink" length="100" /> 
     </key-property> 
     <key-property name="contentsavelocation" type="string"> 
      <column name="contentsavelocation" length="150" /> 
     </key-property> 
     <key-property name="previewsavelocation" type="string"> 
      <column name="previewsavelocation" length="150" /> 
     </key-property> 
    </composite-id> 
    <many-to-one name="user" class="com.paypal.socialpay.models.User" update="false" insert="false" fetch="select"> 
     <column name="id" not-null="true" /> 
    </many-to-one> 
</class> 

回答

1

嘗試使用此:

session.createQuery("from Link where userid=:userId") 
    .setParameter("userId", id) 
    .list() 

更新:我相信映射應該是這樣的:

<id name="id" column="id"> 
     <generator class="native"/> 
    </id> 
    <property name="userid" column="userid"/> 

等等,而不是<key-property> s

查詢不起作用,因爲您試圖從LinkId中選擇 - 這不是實體,而是Link的一個關鍵。可能是一些逆向工程問題。

+0

在運行查詢時發生以下異常:「org.hibernate.QueryParameterException:could not locate named parameter [userId]」 – Hozefa 2012-04-09 23:09:47

+0

@Hozefa請在您的查詢周圍提供更多源代碼。位置參數和命名參數都可以工作。 – 2012-04-09 23:14:15

+0

你叫'createQuery'的對象是什麼?你可以發佈你的映射文件(* .hbm.xml)或LinkId的源文件和註釋嗎? – 2012-04-09 23:24:10