views:

41

answers:

1

I wonder if it's possible to define Set in Hibernate mapping in a such way, that element would specify not column in original (FOO) table, but in joined one (BAR). Let's say we have some FooContainer.hbm.xml, which contains Set of Foo objects:

<set ...>
  <key column="COLUMN_FROM_BAR" />
  <one-to-many class="xyz.Foo" />
</set>

Here FOO has FK to BAR (FOO.BAR_ID), so joining is done through element in Foo.hbm.xml:

<many-to-one class="xyz.Bar" fetch="join" column="BAR_ID" foreign-key="barId" ... />

which results in joined FOO-BAR select whenever xyz.Foo is fetched.

Problem is that where condition of generated Set fetching select is something like:

... WHERE _FOO_0.COLUMN_FROM_BAR = ?

when desired one is:

... WHERE _BAR_0.COLUMN_FROM_BAR = ?
A: 

Found the answer in Java Persistence with Hibernate (page 296). Trick is setting different set table (BAR) from target class (Foo):

  <set table="BAR" ...>
    <key column="COLUMN_FROM_BAR" /> 
    <many-to-many
      class="xyz.Foo"
      column="BAR_ID"
      unique="true"
      />
  </set>
Ralkie