-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Many-to-one relationship where parent class is same as child
PostPosted: Wed Jun 06, 2007 1:26 am 
Newbie

Joined: Wed Jun 06, 2007 1:18 am
Posts: 4
Hibernate version: 3.1.3

Name and version of the database you are using: Oracle 10


Hi I have a question regarding how many-to-one relationship should be defined where the parent class is same type as the child class.

--------------------
Person table
---------------------
- name
- surname
- national_id - (PKey)
- passport_id - (PKey)
- address
- parent_national_id - (Parent -PK)
- parent_passport_id - (Parent -PK)

------------------------

Take the case of the table above where a person is defined within a person table and has a composite key national_id and passport_id.

Now I want to get the Parent for that person which has composite key parent_national_id and parent_passport_id defined within the Person table.

How would the hibernate mapping configuration would look like.

When I do a getParent on a Person object i would want to get its parent of type Person.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 2:36 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Here is the mapping file for your requirment


Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>     
    <class
        name="lk.wts.test.Employee"
        table="Employee"
    >
        <composite-id                      
        >

                     <key-property
                        name="national_id"
                        type="java.lang.String"
                        column="national_id"
                />

                     <key-property
                        name="passport_id"
                        type="java.lang.String"
                        column="passport_id"
                />

        </composite-id>

             <many-to-one
                 name="emp"
                 class="lk.wts.test.Employee"
                 cascade="none"
                 update="false"
                 insert="false"
           
        >
        <column name="parent_national_id" ></column>
        <column name="parent_passport_id"></column>
        </many-to-one>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <property
            name="parent_national_id"
            type="java.lang.String"
            update="true"
            insert="true"
            column="parent_national_id"
        />

        <property
            name="parent_passport_id"
            type="java.lang.String"
            update="true"
            insert="true"
            column="Parent_passport_id"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Employee.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject: works
PostPosted: Wed Jun 06, 2007 6:50 am 
Newbie

Joined: Wed Jun 06, 2007 1:18 am
Posts: 4
Hi ,
thanks Amila it works!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 7:14 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi baadaal


You didn't rate my answer


Amila


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.