当密钥位于父类中时如何将一对多集合映射到已连接的子类 [英] how to map a one-to-many collection to a joined subclass when key is in the parent class

查看:99
本文介绍了当密钥位于父类中时如何将一对多集合映射到已连接的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个到多个集合映射到子类,但集合的关键是父类的一个属性。



目前我是映射AbstractFoo Foo和Bar类,如下所示:

 < class name =AbstractFooabstract =truetable =abstractFoo > 
< id name =_ idcolumn =foo_pk>
< generator class =native/>
< / id>
<多对一名称=_ barcolumn =bar_fk>
< /多对一>
< joined-subclass name =Footable =foo>
< key column =abstractFoo_fk/>
< property name =_ typecolumn =type/>
< / joined-subclass>
< / class>
< class name =Bartable =bar>
< map name =_ foosinverse =true>
< key column =bar_fk/>
< map-key column =type>
<一对多等级=Foo/>
< / map>
< / class>

实际上,当我使用该映射时,Hibernate试图在表foo上找到列bar_fk,而不是



有没有办法做这样的事情?如果我正确理解你的问题,那么我认为这是不可能的。如果类Bar有对Foo的引用,那么使用当前配置生成的FK是正确的。如果你想让Bar有一个对AbstractFoo的引用,那么它将创建FK到abstractFoo表,并允许AbstractFoo的任何子类作为引用bay。通常这是实际上你想要的是对超类的引用。但是,如果你不知道你的设计,如果你真的需要对子类的引用,那么你的配置是正确的。


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.

这篇关于当密钥位于父类中时如何将一对多集合映射到已连接的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆