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.  [ 6 posts ] 
Author Message
 Post subject: Mapping a class to a collection of the same class
PostPosted: Fri Jan 19, 2007 11:59 am 
Newbie

Joined: Wed Sep 06, 2006 2:38 pm
Posts: 6
Would anyone have an example of, or could explain how i would map a class to a collection of itself?

The reason i would like to do this is essentially to create a DAG of the objects and be able to persist it. I have a Job table and a JobDependency table with columns ParentJobID, ChildTaskID that describes all of the edges. When i ask for a Job object from the Db I would like it to come with its children and parent lists populated (recursively). The jobs can appear in multiple lists so ideally i would only want one instance of each job and only references to that instance for its multiple locations to stop the size from exploding.

Any help would be much appreciated, please let me know if i am not being clear,

zoz


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 12:57 pm 
Newbie

Joined: Wed Jan 17, 2007 12:14 pm
Posts: 3
Hi!

If I've understood you clearly - you should do something like this: (my sample)

Code:
   <class name="Office" table="Offices" >
      <id name="Id" column="Id">
         <generator class="identity" />
      </id>
      <bag name="Offices" table="Offices">
         <key column="ParentId"/>
         <many-to-many class="Office" column="Id"/>
      </bag>
   </class>


Each Office Record has a ParentId column, referencing parent node if needed. Supplied mapping creates a collection of children for each office.

Cheers,
Nikolay


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 11:41 am 
Newbie

Joined: Wed Sep 06, 2006 2:38 pm
Posts: 6
Thanks for your help, but Im not sure that the above code will work for the structure I have.

I have a Job table and a jobDependency table as follows

Job - JobID, Status,Description
Job Dependency - ParentJobID,ChildJobID

Jobs can have multiple parents and multiple children, so the table is normalized into the dependency table which just holds all id->id mappings.

Currently i have the following in my mapping file...

Code:
  <class name="Job" table="Job">
    <id name="Id" column="Id" type="Int32" unsaved-value="0">
      <generator class="native"/>
    </id>

    <property column="Status" type="Int32" name="Status" not-null="true" />
    <set name="ChildTasks" table="TaskDependency" lazy="false" inverse="true" cascade ="all" access="NHibernate.Generics.GenericAccessor, NHibernate.Generics">
      <key column="Id"/>
      <one-to-many column="ParentTaskId" class="Job"  />
    </set>
    <many-to-one name="ParentTaskId" column="ChildTaskId" class="Job"  />

  </class>


I'm trying to set up a list map in the usual way only referring it to itself. This is resulting in a MappingException "The 'column' attribute is not declared" and i cant figure it out...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 11:54 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
As there can be many parent tasks for one child, You should replace <many-to-one name="ParentTaskId" column="ChildTaskId" class="Job" /> with <set> or something else that allows one-to-many (instead of many-to-one).

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 1:07 pm 
Newbie

Joined: Wed Sep 06, 2006 2:38 pm
Posts: 6
Ok, i wrote this instead and am no longer getting errors, but the lists are appearing empty when they shouldn't. Do i need to include the other side of each of the mappings in the class also?

Code:
    <set name="ChildTasks"
         table="TaskDependency"
         inverse="true"
         lazy="false"
         access="NHibernate.Generics.GenericAccessor, NHibernate.Generics" >
      <key column="ParentTaskId" />
      <many-to-many
         class="Task"
         column="Id" />
    </set>

    <set name="ParentTasks"
         table="TaskDependency"
         inverse="true"
         lazy="false"
         access="NHibernate.Generics.GenericAccessor, NHibernate.Generics">
      <key column="ChildTaskId" />
      <many-to-many
        class="Task"
        column="Id" />
    </set>



Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 23, 2007 3:52 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
First of all, at elast one set should be inverse="false", otherwise no-one will save new entries.

Second, <many-to-many> element column should not propably be "Id" but the "ParentTaskId" or "ChildTaksId", so that both of them are present in one <set> mapping.

Gert

_________________
If a reply helps You, rate it!


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