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

4
  • Have you tried something? This is pretty straightforward and you could have found an example in one of the many Hibernate tutorials out there. Commented Feb 4, 2013 at 7:32
  • i looked at many examples but all gives same case, in my case we always give PK as id, how to define that user_detail table's field user_id is relates to the user table id field Commented Feb 4, 2013 at 7:49
  • @RaulGogo : I have edited the question to make it clear Commented Feb 4, 2013 at 8:17
  • Posted an answer which actually solves the above requirement. Commented Nov 28, 2018 at 10:29

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

8 Comments

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
did you understand what i need?
@prashu132 modified answer to meet your requirement. Please see above code.
Thanks dude <generator class="foreign"> <param name="property">user</param> </generator> was the thing i needed. Works fine now.
can you give a sample code for creation and updation for user with user_detail
|
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).

Comments

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.

3 Comments

i looked at many examples but all gives same case, in my case we always give PK as id, how to define that user_detail table's field user_id is relates to the user table id field
got any idea about my scenario?
also the link you gave was the first thing i checked while i started got stuck in this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.