Hibernate <set> key from joined table

柔情痞子 提交于 2020-01-03 05:19:11

问题


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

回答1:


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>


来源:https://stackoverflow.com/questions/3733902/hibernate-set-key-from-joined-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!