当键位于父类中时,如何将一对多集合映射到连接的子类
我想将一对多集合映射到子类,但集合的键是父类的属性。
目前,我正在映射 AbstractFoo Foo 和 Bar 类,如下所示:
<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>
实际上,当我使用该映射时,Hibernate 试图在表 foo 而不是 AbstractFoo 上找到列 bar_fk 。
有什么办法可以做到这样的事情吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你的问题,那么我认为这是不可能的。如果类 Bar 具有对 Foo 的引用,则使用当前配置生成的 FK 是正确的。如果您希望 Bar 拥有对 AbstractFoo 的引用,那么它将创建抽象Foo 表的 FK,并允许 AbstractFoo 的任何子类引用 Bay Bar。
通常这实际上就是您想要的......对超类的引用。然而,在不了解更多关于您的设计的情况下,如果您确实需要对子类的引用,那么您的配置是正确的。
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.