Hibernate one-to-one mapping with a reference column (XML mapping)

陌路散爱 提交于 2019-12-01 00:08:29

For User mapping....

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

For UserDetail mapping.

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

In the User.hbm you need to declare the relation with UserDetails like this:

<one-to-one name="user_detail" foreign-key="user_id" class="UserDetail full name "/>

Hope this help you

For one-to-one associations where primary key of UserDetail is not the foreign key, we have to represent it as many-to-one association on the owning side.

From Hibernate docs https://docs.jboss.org/hibernate/orm/3.2/reference/en/html/mapping.html

Alternatively, a foreign key with a unique constraint, from Employee to Person, may be expressed as:

<many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>

And this association may be made bidirectional by adding the following to the Person mapping:

<one-to-one name"employee" class="Employee" property-ref="person"/>

So, there is no way to map this association using one-to-one, you have to change the mapping to many-to-one on the owning side (Table holding the foreign key).

<one-to-one name="user_detail" class="give the full specified className with package declaration" cascade="save-update"></one-to-one>

Here is an good example

http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/

please find it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!