gpt4 book ai didi

java - 编写外键关联查询

转载 作者:行者123 更新时间:2023-12-01 15:26:23 25 4
gpt4 key购买 nike

我在数据库用户和链接中有 2 个表。 userid 在链接表中是外来的。我已经使用 hibernate reverse engg xml 在 java 中创建模型类。它创建了User、Link、LinkId类。这里Link类链接了2个表,而LinkId包含了Link的属性。我正在尝试使用用户 ID 查询链接表。我的查询是“createQuery(“来自 com.paypal.socialpay.models.LinkId li,其中 li.userid=?").setInteger(0, id).list();"

但是在执行查询时,我得到“java.lang.IllegalArgumentException:查询中没有位置参数:来自 com.paypal.socialpay.models.LinkId li,其中 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>

最佳答案

尝试使用这个:

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>

查询不起作用,因为您尝试从 LinkId 中进行选择 - 它不是实体,而是链接的键。可能是一些逆向工程问题。

关于java - 编写外键关联查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10080766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com