how to map a one-to-many collection to a joined subclass when key is in the parent class

三世轮回 提交于 2019-12-10 10:48:36

问题


I'd like to map a one to many collection to a subclass but the key of the collection is an attribute of the parent class.

Currently i'm mapping AbstractFoo Foo and Bar class like this :

<class name="AbstractFoo" abstract="true" table="abstractFoo">
  <id name="_id" column="foo_pk">
    <generator class="native" />
  </id>
  <many-to-one name="_bar" column="bar_fk">
  </many-to-one>
  <joined-subclass name="Foo" table="foo">
    <key column="abstractFoo_fk" />
    <property name="_type" column="type" />
  </joined-subclass>
</class>
<class name="Bar" table="bar">
  <map name="_foos" inverse="true">
    <key column="bar_fk"/>
    <map-key column="type">
    <one-to-many class="Foo" />
  </map>
</class>

Actually when i work with that mapping Hibernate is trying to found the column bar_fk on the table foo instead of abstractFoo.

Is there any way to do such a thing ?


回答1:


If I understand your question correctly then I don't think that is possible. If the class Bar has a reference to Foo then the FK that is generated with the current config is correct. If you want Bar to have a reference to AbstractFoo then it will create the FK to the abstractFoo table and allow any subclass of AbstractFoo to be references bay Bar.

Usually this is actually what you want...A reference to the super class. However, without knowing moree about your design, if you reallly jst need a reference to the subclass then your configuration is correct.



来源:https://stackoverflow.com/questions/1940331/how-to-map-a-one-to-many-collection-to-a-joined-subclass-when-key-is-in-the-pare

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