小编典典

带有参考列的休眠一对一映射(XML映射)

hibernate

我有一个用户表和一个user_detail具有一对一映射user_detail表的表,其中一个字段user_id用于此关系,该字段存储相应用户的ID字段值。

如何hbm为此关系编写hibernate文件?

更新

我的问题是用户的主键是id,user_detail的外键是user_id

我在Internet用户user_id中获得的所有示例均作为用户主键,与其他表中的外键相同


阅读 240

收藏
2020-06-20

共1个答案

小编典典

对于用户映射…。

<?xml version="1.0"?>
        <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
        <!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 -->
        <hibernate-mapping>
            <class name="com.rais.User" table="USER" catalog="mydb">
                <id name="userId" type="java.lang.Integer">
                    <column name="USER_ID" />
                    <generator class="identity" />
                </id>
                <property name="userName" type="string">
                    <column name="USER_NAME" length="10" not-null="true" unique="true" />
                </property>
                <property name="userCode" type="string">
                    <column name="USER_CODE" length="20" not-null="true" unique="true" />
                </property>
                <one-to-one name="userDetail" class="com.rais.UserDetail"
                    cascade="save-update"></one-to-one>
            </class>
        </hibernate-mapping>

用于UserDetail映射。

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping>
        <class name="com.rais.UserDetail" table="USER_DETAIL"
            catalog="mydb">
            <id name="userId" type="java.lang.Integer">
                <column name="USER_ID" />
                <generator class="foreign">
                    <param name="property">user</param>
                </generator>
            </id>
            <one-to-one name="user" class="com.rais.User"
                constrained="true"></one-to-one>
            <property name="compName" type="string">
                <column name="COMP_NAME" length="100" not-null="true" />
            </property>
            <property name="compDesc" type="string">
                <column name="COMP_DESC" not-null="true" />
            </property>
            <property name="remark" type="string">
                <column name="REMARK" not-null="true" />
            </property>
        </class>
    </hibernate-mapping>
2020-06-20