-->
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: how to obtain a tree of objects from the same table?
PostPosted: Tue Jul 07, 2009 11:14 am 
Newbie

Joined: Fri Jun 26, 2009 2:46 pm
Posts: 2
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       
------------------------------------------------------


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.