René Pöpperl
René Pöpperl

Reputation: 617

Hibernate: Mapping with one-to-many on non-primary-key columns

I'm stuck at the hibernate xml mapping configuration.

I've established some tables with foreign key constraints in my MSSQL database:

Table ItemsBase 
ID       int   primary-key
ItemID   int   unique index
... some more columns

Table Others
ID       int   primary-key
ItemID   int   unique index
... some more columns

The foreign key constraint is configured to connect these two tables by using the column "ItemID".

My ItemsBase.hbm.xml files looks like:

<hibernate-mapping>
    <class name="de.delife.sql.ItemsBase" table="ItemsBase" schema="dbo" catalog="Delife_Plenty">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id> 
        <property name="itemId" type="java.lang.Integer">
            <column name="ItemID" unique="true" />
        </property>
        <set name="otherses" table="Others" inverse="true" lazy="true" fetch="select">
            <key property-ref="itemId">
                <column name="ItemID" />
            </key>
            <one-to-many class="de.delife.sql.Others" not-found="ignore" />
        </set>
    </class>
</hibernate-mapping>

and the Others.hbm.xml files looks like:

<hibernate-mapping>
    <class name="de.delife.sql.Others" table="Others" schema="dbo" catalog="Delife_Plenty">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <many-to-one name="itemsBase" class="de.delife.sql.ItemsBase" fetch="select" property-ref="itemId">
            <column name="ItemID" unique="true" />
        </many-to-one>
    </class>
</hibernate-mapping>

Everything looks fine for me, but when I run my program I get a hibernate error:

property [itemId] not found on entity [de.delife.sql.Others]

I have an established relation between ItemsBase and a table named ItemsProperties and it works, but with this "pretty" table "Others" I'm stuck.

I would be glad, if someone can help me on this matter. Thx in advance.

Upvotes: 5

Views: 9535

Answers (2)

Aksanth
Aksanth

Reputation: 319

Try like this. I tried and its working for me.

<set name="otherses" table="Others"  inverse="true" lazy="true" fetch="select">
    <key column="itemId" foreign-key="itemId" property-ref="itemId"/>
    <one-to-many class="de.delife.sql.Others" not-found="ignore" />
</set> 

Upvotes: 2

aquaraga
aquaraga

Reputation: 4168

Item id should be defined as a property in Others.hbm.xml:

<property name="itemId" column="ItemId"/>

Upvotes: 0

Related Questions