views:

13

answers:

1

Hello.

I have the following entity in my HBM file:

<join table="v_price_change_current_prices" fetch="join" inverse="true">
    <key column="product_color_id" />
    <property name="oldMSRP" column="msrp" />
    <property name="oldList"  column="list" />
</join>

my PRICE_CHANGE table has an ID column (primary key) and a PRODUCT_COLOR_ID column.

I would like the SQL Hibernate produces to join V_PRICE_CHANGE_CURRENT_PRICES to PRICE_CHANGE.PRODUCT_COLOR_ID, like so:

    SELECT * from PRICE_CHANGE a, V_PRICE_CHANGE_CURRENT_PRICES b 
where a.product_color_id=b.product_color_id

But its joining to PRICE_CHANGE.ID instead

    SELECT * from PRICE_CHANGE a, V_PRICE_CHANGE_CURRENT_PRICES b 
where a.product_color_id=b.product_color_id

Is there any way to tell the element to to use PRICE_CHANGE.PRODUCT_COLOR_ID instead of PRICE_CHANGE.ID?

+1  A: 

According to the documentation, this might be doable using a property-ref in the <key> element:

5.1.21. Key

The <key> element has featured a few times within this guide. It appears anywhere the parent mapping element defines a join to a new table that references the primary key of the original table. It also defines the foreign key in the joined table:

<key
        column="columnname"                      (1)
        on-delete="noaction|cascade"             (2)
        property-ref="propertyName"              (3)
        not-null="true|false"                    (4)
        update="true|false"                      (5)
        unique="true|false"                      (6)
/>
  1. column (optional): the name of the foreign key column. This can also be specified by nested element(s).
  2. on-delete (optional - defaults to noaction): specifies whether the foreign key constraint has database-level cascade delete enabled.
  3. property-ref (optional): specifies that the foreign key refers to columns that are not the primary key of the original table. It is provided for legacy data.
  4. not-null (optional): specifies that the foreign key columns are not nullable. This is implied whenever the foreign key is also part of the primary key.
  5. update (optional): specifies that the foreign key should never be updated. This is implied whenever the foreign key is also part of the primary key.
  6. unique (optional): specifies that the foreign key should have a unique constraint. This is implied whenever the foreign key is also the primary key.

But I'm not sure this is actually implemented (see HHH-551, HHH-1829).

An alternative if you really don't want to use a shared primary key would be to use composition and a "real" one-to-one association instead.

References

Pascal Thivent