I have an issue,
Im using PostgreSQL 8.2 and I want to obtain a tree of objects from the same table.
The table is:
Code:
---------------------------------------
Location
---------------------------------------
Id_Location
Name
Description
Id_Location_Father
---------------------------------------
My Location class:
Code:
public class Location implements Serializable{
private static final long serialVersionUID = 1L;
private Location father;
private Set<Location> childrens;
private Long id;
private String name;
private String description;
/*** getters and setters ***/
}
And I want to map the childrens property as a select of all the Location that have the column
Id_Location_Father equal to Id_Location column. In PostgreSQL when I run this query:
Code:
SELECT
"Location1"."Id_Location",
"Location1"."Id_Location_Father",
"Location1"."name",
"Location1"."Description"
FROM
public."Location",
public."Location" "Location1"
WHERE
(public."Location"."Id_Location" = "Location1"."Id_Location_Father")
It works fine, but when I use it in the mapping it does not return the same result.
Table data:
Code:
------------------------------------------------------
Location
------------------------------------------------------
Id_Location | Id_Location_Father | Name | Description
1 Null 1 1
2 1 2 2
3 1 3 3
------------------------------------------------------
Query from PostgreSQL result:
Code:
------------------------------------------------------
Location
------------------------------------------------------
Id_Location | Id_Location_Father | Name | Description
2 1 2 2
3 1 3 3
------------------------------------------------------
My mapping file:
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="offimant.catalogo.domain">
<class name="Location" table="`Location`" lazy="false">
<id name="id" column="`Id_Location`" type="long">
<generator class="increment"/>
</id>
<property name="name" column="`Name`" type="string" not-null="true" unique="true"/>
<property name="description" column="`Description`" type="string"/>
<many-to-one name="father" class="Location" column="`Id_Location_Father`"/>
<set name="childrens" table="`Location`" >
<subselect>
SELECT
"Location1"."Id_Location",
"Location1"."Id_Location_Father",
"Location1"."name",
"Location1"."Description"
FROM
public."Location",
public."Location" "Location1"
WHERE
(public."Location"."Id_Location" = "Location1"."Id_Location_Father")
</subselect>
<synchronize table="`Location`"/>
<key column="`Id_Location`"/>
<one-to-many class="Location"/>
</set>
</class>
</hibernate-mapping>
Query from Hibernate mapping result:
Code:
------------------------------------------------------
Location
------------------------------------------------------
Id_Location | Id_Location_Father | Name | Description
1 1 1 1
2 2 2 2
3 3 3 3
------------------------------------------------------