gpt4 book ai didi

java - Hibernate加载同一行

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

我有一张 table

canvastowidgets
{
idCanvas int PK
idWidgets int PK
version varchar
sequence int
position int
inPanelOrNot int
}

映射 xml 如下

<hibernate-mapping>
<class name="com.config.canvas.CanvasHasWidget" table="canvastowidgets">
<id name="canvasId" type="int">
<column name="idCanvas" />
<generator class="assigned" />
</id>
<property name="widgetId" type="int">
<column name="idWidgets" />
</property>
<property name="sequence" type="int">
<column name="sequence" />
</property>
<property name="position" type="int">
<column name="position" />
</property>
<property name="inPanel" type="boolean">
<column name="inPanelOrNot" />
</property>
</class>
</hibernate-mapping>

当我执行如下查询时

Query query = session.createQuery("from CanvasHasWidget where idCanvas = :id");
query.setParameter("id",canvas.getCanvasId());
return query.list();

查询是

select canvashasw0_.idCanvas as idCanvas1_2_, canvashasw0_.idWidgets as idWidget2_2_, canvashasw0_.sequence as sequence3_2_, canvashasw0_.position as position4_2_, canvashasw0_.inPanelOrNot as inPanelO5_2_ from canvastowidgets canvashasw0_ where idCanvas=?

07:38:16,822 TRACE BasicBinder:81 - binding parameter [1] as [INTEGER] - [1]
07:38:16,829 TRACE BasicExtractor:78 - extracted value ([idCanvas1_2_] : [INTEGER]) - [1] 07:38:16,835 TRACE BasicExtractor:78 - extracted value ([idCanvas1_2_] : [INTEGER]) - [1] 07:38:16,840 TRACE BasicExtractor:78 - extracted value ([idCanvas1_2_] : [INTEGER]) - [1]

列表返回 3 行,其中表中有 3 行。但返回的所有 3 行都是相同的。

最佳答案

问题是您的表有一个复合主键,如下所示:

idCanvas int PK
idWidgets int PK

但您只映射了其中之一。更改您的映射并将 id 部分替换为

<composite-id>
<key-property name="idCanvas"/>
<key-property name="idWidgets"/>
</composite-id>

关于java - Hibernate加载同一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23851409/

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