Java 编写外键关联查询
我在数据库用户和链接中有2个表。用户标识在链接表中是外来的。我已经使用hibernate反向工程xml在java中创建了模型类。 它已创建用户、链接、LinkId类。在这里,Link类链接这两个表,而LinkId包含Link的属性。 我正在尝试使用userid查询链接表。我的查询是“createQuery”( “来自com.paypal.socialpay.models.LinkId li,其中li.userid=?”).setInteger(0,id.list();” 但在执行查询时,我得到“java.lang.IllegalArgumentException:查询中没有位置参数:来自com.paypal.socialpay.models.LinkId li,其中li.userid=?” 有人能告诉我我做错了什么吗Java 编写外键关联查询,java,sql,hibernate,Java,Sql,Hibernate,我在数据库用户和链接中有2个表。用户标识在链接表中是外来的。我已经使用hibernate反向工程xml在java中创建了模型类。 它已创建用户、链接、LinkId类。在这里,Link类链接这两个表,而LinkId包含Link的属性。 我正在尝试使用userid查询链接表。我的查询是“createQuery”( “来自com.paypal.socialpay.models.LinkId li,其中li.userid=?”).setInteger(0,id.list();” 但在执行查询时,我得到“
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>
class name=“com.paypal.socialpay.models.Link”table=“Link”catalog=“socialdb”>
尝试使用以下方法:
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"/>
等,而不是
s
查询不起作用,因为您正在尝试从LinkId中进行选择,LinkId不是实体,而是链接的键。可能是逆向工程问题。运行查询时出现以下异常“org.hibernate.QueryParameterException:找不到命名参数[userId]”@Hozefa请提供有关查询的更多源代码。位置参数和命名参数都应该工作。您从哪个对象调用“createQuery”?是否可以发布映射文件(*.hbm.xml)或带有批注的LinkId的源文件?@EugeneRetunsky hbm.xml已添加。