-->
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.  [ 1 post ] 
Author Message
 Post subject: Problems with reflexive foreign key
PostPosted: Thu May 22, 2008 3:53 am 
Newbie

Joined: Thu May 22, 2008 3:32 am
Posts: 1
Greetings,

I'm trying to solve one of the most-common asked questions about reflexive foreign keys, without success.

I have a classic PARENT-CHILD table like this:

Code:
TABLE  Resources
ID_RESOURCE NUMBER(9)
CODE_RESOURCE   VARCHAR2(30)
ID_PARENT_RESOURCE  NUMBER(9)
PK : ID_RESOURCE
FK : ID_PARENT_RESOURCE references Resources (ID_RESOURCE)


Let's supose several rows inserted in that table:

Code:
1,'ABCDE',null
2,'12345',1
3,'A1B1C1',1


So resource 1 has two child resources: 2 & 3.

My mapping is the following one:

Code:
<hibernate-mapping>
    <class name="com.foo.Resources" table="RESOURCES" schema="MY_SCHEMA">
        <id name="idResource" type="int">
            <column name="ID_RESOURCE" precision="9" scale="0" />
            <generator class="assigned" />
        </id>
       <property name="codeResource" type="string">
            <column name="CODE_RESOURCE" length="30" not-null="true"  />
        </property>         
        <many-to-one name="parentResource" class="com.foo.Resources" fetch="select">
            <column name="ID_PARENT_RESOURCE" precision="9" scale="0" not-null="false" />
         </many-to-one>       
       
        <set name="childResources" inverse="true" table="RESOURCES"  fetch="select">
             <key>
               <column name="ID_PARENT_RESOURCE" precision="9" scale="0" not-null="false" />
            </key>             
            <one-to-many class="com.foo.Resources" />
        </set>     
    </class>
</hibernate-mapping>


My DTO class, com.foo.Resources is coded like this:

Code:
package com.foo;
import blahblahblah;

public class Resources implements java.io.Serializable {

   private static final long serialVersionUID = 1L;

   private int idResource;
   private String codeResource;
   private Resources parentResource;
   private Set childResources = new HashSet(0);

   //toons of getters and setters

}


Well, if i have a valid instance of, for example resource=2, and I ask for its parent:

Code:
resource2.getParentResource();


it works fine!!! Resource 1's reference is returned.

HOWEVER, when i'm trying to get all child resources of resource=1:

Code:
Set set = resource1.getChildResources();


I always get an empty set. Having (show_sql=true) does not output any SQL query. It seems like Hibernate is ignoring me!

I need something like:

SELECT * FROM RESOURCES WHERE ID_PARENT_RESOURCE=1

Which gives me a set of two com.foo.Resources objects!!!

Could anybody help me? THANKS IN ADVANCE

Joan


Hibernate version:
3.0

Mapping documents:



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


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

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.