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