2012-04-09 58 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