views:

97

answers:

1

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

<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>
A: 

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.

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.

There's one issue you may ran into with shared queries (i.e. 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. Otherwise, everything should work pretty much as you expect.

schmeedy
Thanks for that.
Jamie Penney