Hibernate对象映射关系。多态

  • 3种策略 

hibernate中解决映射对象存在多态关系是一般使用3种策略。  

  •  
    • 共享同一个表

在该策略下,父类和子类数据都在一个表中。假设有3个类Container、Box、Bottle。其中Container<-Box,Container<-Bottle。对于这种策略,一般是使用discriminator。

<hibernate-mapping>
    <class name="Container">
        <id name="containerId">
            <generator class="hilo"/>
        </id>
        <discriminator column="CONTAINER_TYPE" type="string"/>
        <property name="size"/>
        <property name="name"/>

        <subclass name="Box" discriminator-value="BOX">
            <property name="height"/>
            <property name="length"/>
            <property name="width"/>
        </subclass>

        <subclass name="Bottle" discriminator-value="BOTTLE">
             <property name="diameter"/>
             <property name="height"/>
         </subclass>

    </class>
</hibernate-mapping>

猜你喜欢

转载自stark-t.iteye.com/blog/1558660