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