-->
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.  [ 5 posts ] 
Author Message
 Post subject: Parent child relation in single table
PostPosted: Fri Jun 01, 2007 11:02 am 
Beginner
Beginner

Joined: Wed May 16, 2007 7:12 am
Posts: 41
Location: London
Hi,

I am using Hibernate 2.

I have a table with the structure

ID PARENT_ID NAME
---------------------------------------

what is the best way to represent this as a hibernate mapping, so I can move down the tree to get the children of each parent. The legacy table structure is fixed.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 3:51 pm 
Newbie

Joined: Tue Jul 26, 2005 10:25 am
Posts: 12
Here's how we implemented an Organization Tree. We used a <many-to-one> relationship that worked well for us.

Jason

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="mil.af.amc.acap.model" default-lazy="true" default-access="field">

    <class name="Organization" table="T_ORGANIZATION">

        <cache usage="read-write" />

        <id name="id" column="ORGANIZATIONID">
            <generator class="increment"/>
        </id>

        <property name="name" column="ORGANIZATIONNAME"/>

        <many-to-one name="parent" column="PARENTID" />
        <many-to-one name="organizationType" column="ORGANIZATIONTYPEID" />

        <set name="childOrganizations" table="T_ORGANIZATION" inverse="true" sort="natural" lazy="false">
            <cache usage="read-write" />
            <key column="PARENTID"/>
            <one-to-many class="Organization" />
        </set>

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 9:34 am 
Beginner
Beginner

Joined: Wed May 16, 2007 7:12 am
Posts: 41
Location: London
yes, that is just what i did, the many to one and one to many relationship from the same table.
I was using the wrong column for the one to many relation.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 5:01 pm 
Newbie

Joined: Mon Jun 11, 2007 4:40 pm
Posts: 1
Hi,

Is it possible to do this when the parent-child relationship involved joined-subclasses?

We have a similar situation in that the parent-child relationship is defined in a single table for a superclass Entity:

ID PARENT_ID NAME
-------------------------------------
1 0 parent
2 1 child one
3 1 child two

However, we have two tables for classes that extend the base class, Thing and ChildThing, something like this:

ID EXTP_FIELD1 EXTP_FIELD2
------------------------------------------------
1 extended parent ext parent desc.

ID EXTC_FIELD1 EXTC_FIELD2
-------------------------------------------------------
2 extended child one ext child one desc.
3 extended child two ext child two desc.

The subclasses are such that the Thing has a Set of ChildThing objects. But these are known by the PARENT_ID column in the Entity table, not from any field in the Thing or ChildThing table. When I try to set up a
one-to-many relationship in the Thing mapping using PARENT_ID as the key, Hibernate attempts to get PARENT_ID from the ChildThing table instead of the Entity table where it actually lives.

This is the mapping for the Thing class:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <joined-subclass name="ext.Thing" extends="base.Entity" table="THING">
      <key column="ID"/>
      <property name="extpField1" column="EXTP_FIELD1"/>
      <property name="extpField2" column="EXTP_FIELD2"/>
        <set name="childthings" lazy="false" table="CHILDTHING">
                <key column="PARENT_ID" not-null="true"/>
                <one-to-many class="ext.ChildThing"/>
        </set>
   </joined-subclass>
</hibernate-mapping>


and for the Entity class:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="base.Entity" table="ENTITY">
      <id name="id" column="ID" type="string"/>
      <property name="parentId" type="string" not-null="true" column="PARENT_ID"/>
      <property name="name" type="string" not-null="true" column="NAME" />
   </class>
</hibernate-mapping>



The mapping file for ChildThing looks nearly identical to the Thing mapping file.

Any idea how I can get Hibernate to take the one-to-many parent key from the Entity table and not the ChildThing table?


Top
 Profile  
 
 Post subject: Can I also get the annotation version of this XML
PostPosted: Wed Jun 13, 2007 2:00 pm 
Newbie

Joined: Sun Apr 22, 2007 5:49 pm
Posts: 5
I have never used xml based hibernate mapping. Please can any one transform the above implementation into a set of hibernate annotation. I would appreciate it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.