简体   繁体   English

NHibernate中的继承和关联

[英]Inheritance and Associations in NHibernate

I have an entity model where the base class in an inheritance structure has an association with another class, and was wondering if the subtypes of the base class will have the association mapped up as well? 我有一个实体模型,其中继承结构中的基类与另一个类有关联,并且想知道基类的子类型是否也会映射关联?

For a bit more information, here is a basic outline of this part of the system: 有关更多信息,这是系统此部分的基本概述:

Transport is the base class, and has an association with Owner. 运输是基类,并且与所有者有关联。 Bike and Car are two of the subclasses. 自行车和汽车是两个子类。

They are represented in 3 tables with the same names using Table Per Subclass inheritance structure. 使用“每个子类的继承”结构在3个具有相同名称的表中表示它们。 The Transport table holds the foreign key reference to Owner. 传输表包含对所有者的外键引用。

This is how I this the mapping should work, am I correct? 这就是我该映射应该如何工作的方式,对吗? I haven't seen anything around addressing this so I thought it would be a good question for SO. 关于解决这个问题,我什么都没看到,所以我认为这对SO是个好问题。

<class name="Transport" table="TRANSPORT">
    <id name="Id" type="Int64" column="Transport_ID">
        <generator class="native"/>
    </id>

    <many-to-one name="Owner" column="Owner_ID" /> 

    <joined-subclass name="Bike" table="BIKE">
        <key column="Bike_ID"/>
    </joined-subclass>
    <joined-subclass name="Car" table="CAR">
        <key column="Car_ID"/>
    </joined-subclass>
</class>

I've only used hibernate for Java, so some specific things may not apply to your case...but I assume it would be quite simmilar. 我只使用过Java的休眠模式,因此某些特定的内容可能不适用于您的情况……但是我认为这很简单。

All mapped properties (including associations) from superclass are accessible in ancestors. 父类中可以访问超类的所有映射属性(包括关联)。 Note that you can have superclasses that are not mapped in XML or annotated - properties from these superclasses are not stored in database at all. 请注意,您可以具有未映射为XML或未注释的超类-这些超类的属性根本不会存储在数据库中。

There's one issue you may ran into with shared queries (ie queries for the Transport type) combined with lazy loading where in some cases hibernate creates proxies exclusively for the supertype that you cannot use to access any property from the ancestor. 共享查询(即对传输类型的查询)与延迟加载结合在一起可能会遇到一个问题,在某些情况下,hibernate会为该超类型专门创建代理,而您不能使用该代理来访问祖先的任何属性。 Otherwise, everything should work pretty much as you expect. 否则,一切都会按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM