Hibernate加载相同的行

时间:2014-05-25 02:16:46

标签: java hibernate hql

我有一张桌子

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 - 绑定参数[1]为[INTEGER] - [1]
  07:38:16,829 TRACE BasicExtractor:78 - 提取值([idCanvas1_2_]:[INTEGER]) - [1]   07:38:16,835 TRACE BasicExtractor:78 - 提取值([idCanvas1_2_]:[INTEGER]) - [1]   07:38:16,840 TRACE BasicExtractor:78 - 提取值([idCanvas1_2_]:[INTEGER]) - [1]

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

1 个答案:

答案 0 :(得分:1)

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

idCanvas int PK
idWidgets int PK

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

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