chiranjeevigk
chiranjeevigk

Reputation: 1666

Hibernate load same row

I have a table

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

and the mapping xml as below

<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>

When I execute the query as below

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

query is

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]

The list returns 3 rows where I have 3 rows in table. but all 3 rows which returned are same.

Upvotes: 0

Views: 69

Answers (1)

Ean V
Ean V

Reputation: 5439

The problem is that your table has a composite primary key as follow:

idCanvas int PK
idWidgets int PK

but you have only mapped one of them. Change your mapping and replace the id section with

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

Upvotes: 1

Related Questions