小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

hibernate的繼承類的映射

 Long_way 2007-04-25
Root.java{Long id;Long Version;String code}                                                                                         

Child1.java extends Root{String a;}

Child2.java extends Roor{String b;}                       

1.直接映射,2個(gè)類Child1,Child2分別映射2個(gè)表

<hibernate-mapping>
    <class name="Root" table="CHILD1">
        <id name="id" column="CHILD1_ID" type="long">
            <generator class="native">
                <param name="sequence">root_sequence</param>
                <param name="parameters">START WITH 1000</param>
            </generator>
        </id>
        <version name="version" column="VERSION" type="long"/>
        <property name="code" type="string">
            <column name="CODE" not-null="true" length="100" unique-key="UK_ROOT"/>
        </property>
        <property name="a" column="A" type="string"></property>  
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="Root" table="CHILD2">
        <id name="id" column="CHILD2_ID" type="long">
            <generator class="native">
                <param name="sequence">root_sequence</param>
                <param name="parameters">START WITH 1000</param>
            </generator>
        </id>
        <version name="version" column="VERSION" type="long"/>
        <property name="code" type="string">
            <column name="CODE" not-null="true" length="100" unique-key="UK_ROOT"/>
        </property>
        <property name="a" column="A" type="string"></property>  
    </class>
</hibernate-mapping>

這是最簡(jiǎn)單的映射策略,隱式多態(tài),查詢時(shí)面向?qū)ο?,查詢Root,會(huì)把其子類Child1,Child2的表都搜索

2。有joined-subclass,將公共字段放在一張表root上,child1表上存id,a;child2表存id,b;其中id字段與root上的字段關(guān)聯(lián);共3張表

<hibernate-mapping>
    <class name="Root" table="ROOT">
        <id name="id" column="ROOT_ID" type="long">
            <generator class="native">
                <param name="sequence">root_sequence</param>
                <param name="parameters">START WITH 1000</param>
            </generator>
        </id>
        <version name="version" column="VERSION" type="long"/>
        <property name="code" type="string">
            <column name="CODE" not-null="true" length="100" unique-key="UK_ROOT"/>
        </property>
        <joined-subclass name="Child1" table="CHILD1">
            <key column="id"></key>
            <property name="a" type="string">
                <column name="A" not-null="true" unique-key="UK_CHILD1"/>
            </property>
        </joined-subclass>
        <joined-subclass name="Child2" table="CHILD2">
            <key column="id"></key>
            <property name="b" type="string">
                <column name="B" not-null="true" unique-key="UK_CHILD2"/>
            </property>
        </joined-subclass>
    </class>
</hibernate-mapping>

其中joined-subclass下的key表示外鍵,查詢Child1的時(shí)候,會(huì)將root表與child1聯(lián)合,效率也不高

3.采用discriminator,只有一張表

<hibernate-mapping>
    <class name="Root" table="ROOT">
        <id name="id" column="ROOT_ID" type="long">
            <generator class="native">
                <param name="sequence">root_sequence</param>
                <param name="parameters">START WITH 1000</param>
            </generator>
        </id>
        <discriminator  column="TYPE" type="string"></discriminator>
        <version name="version" column="VERSION" type="long"/>
        <property name="code" type="string">
            <column name="CODE" not-null="true" length="100" unique-key="UK_ROOT"/>
        </property>
        <subclass name="Child1" discriminator-value="1">
            <property name="a" column="A" type="string"></property>
        </subclass>
        <subclass name="Child2" discriminator-value="2">
            <property name="b" column="B" type="string"></property>
        </subclass>
    </class>
</hibernate-mapping>

注意的是discriminator 要在id和version之間,而且hbm文件都必須有dtd。這樣的查詢效率高多了,查詢child1的時(shí)候會(huì)自動(dòng)根據(jù)條件type=1


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多