5

I have a user table and a user_detail table with one to one mapping user_detail table have a field user_id to be used for this relation which stores id field value of corresponding user.

How to write the hibernate hbm file for this relation?

UPDATE

what my problem is that user's primary key is id , user_detail's foreign key is user_id

all the examples i got in internet users user_id as users primary key and the same as foreign key in other table

Prashanth Shyamprasad
  • 827
  • 2
  • 17
  • 39

4 Answers4

6

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

EGo X
  • 101
  • 1
  • 4
  • Now how do we get "delete" to work if user_detail is null when calling update()? Tried `cascade="all"` and `cascade="all,delete-orphan"` but neither work – Collin Jan 28 '23 at 22:19
3

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>
Rais Alam
  • 6,970
  • 12
  • 53
  • 84
  • its many-to-one relation na, i need one-to-one, also users primary key name is id and the foreign key in user_detail is user_id – Prashanth Shyamprasad Feb 04 '13 at 07:46
  • did you understand what i need? – Prashanth Shyamprasad Feb 04 '13 at 08:16
  • @prashu132 modified answer to meet your requirement. Please see above code. – Rais Alam Feb 04 '13 at 08:38
  • Thanks dude ` user ` was the thing i needed. Works fine now. – Prashanth Shyamprasad Feb 04 '13 at 08:49
  • can you give a sample code for creation and updation for user with user_detail – Prashanth Shyamprasad Feb 04 '13 at 09:41
  • a very good example here. http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-one-to-one-using-annotations-1.html – Rais Alam Feb 04 '13 at 09:50
  • i have tried `UserDetail userdetail= new UserDetail(); userdetail.setUser(user); ... other values.... user.setUserDetail(userdetail); update(user);` ... Its fine when user details is new, when i try again (user detail already created) its gives error instead of updating values. Error is , **org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:** – Prashanth Shyamprasad Feb 04 '13 at 10:24
  • @prashu132 Why has the answer been accepted? I though your requirement was Having user_detail_id as primary key in UserDetail and a separate column user_id as foreign key. This answer uses same column for both primary and foreign key in UserDetail. – Sumit Jain Nov 28 '18 at 10:14
3

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).

Sumit Jain
  • 1,484
  • 2
  • 25
  • 44
0
<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.

Rakesh Mahapatro
  • 866
  • 5
  • 12