美文网首页
一对一关系

一对一关系

作者: 打死你的小乌龟 | 来源:发表于2018-01-19 14:51 被阅读0次

    映射文件

    <hibernate-mapping>
        <class name="com.hw.entity.Course">
            <id name="cid" length="5">
                <generator class="increment"></generator>
            </id>
            <property name="name" length="20"></property>
            <!-- 
                unique  外键是唯一的  添加了一个唯一的约束
             -->
            <many-to-one name="person" column="pid" 
                class="com.hw.entity.Person" 
                unique="true"></many-to-one>
        </class>
    </hibernate-mapping>
    
    <hibernate-mapping>
        <class name="com.hw.entity.Person">
            <id name="pid" length="5">
                <generator class="increment"></generator>
            </id>
            <property name="name" length="20"></property>
            <property name="sex" length="20"></property>
            <set name="courses" cascade="save-update">
                <key>
                    <column name="pid"></column>
                </key>
                <one-to-many class="com.hw.entity.Course"/>
            </set>
        </class>
    </hibernate-mapping>
    

    相当于一对多的特例

    相关文章

      网友评论

          本文标题:一对一关系

          本文链接:https://www.haomeiwen.com/subject/xtwooxtx.html