Hibernate one to many using something other than a

2019-06-18 23:46发布

问题:

I have a class A that has a set of B's. However, these two objects are linked by fields that are NOT the primary key.

For B, I can use <key column>, but how do I specify that the join should be in A.secondary_column? Not A.table_primary_key_id ?

<class table="a">
    <id column="table_primary_key_id">
    </id>
    <property column="secondary_column" />

    <set table="B" lazy="false" >
        <key column="B_not_primary" />
        <one-to-many class="BClass" />
    </set>
</class>    

回答1:

Solved with

<set name="someSet" table="B" lazy="false">
    <key column="B_not_primary" property-ref="secondary_column" />
    <one-to-many class="BClass" />
</set>