-->
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.  [ 7 posts ] 
Author Message
 Post subject: getting parent info from child
PostPosted: Fri Jan 22, 2010 2:46 pm 
Newbie

Joined: Thu Dec 03, 2009 2:10 pm
Posts: 13
Hello,
I have 2 mappings defined - parent and child. I am using the following code to retrieve the child info, and this is working fine -

Code:
IList<child> c = myProvider.getChildren();

foreach(var item in c)
{
Console.Writeline(item.myItem);
}


What I need to do, is to get at the parent information for that child. I thought I would be able to do this -

Code:
IList<child> c = myProvider.getChildren();

foreach(var item in c)
{
Console.Writeline(item.parent.myParentItem);
}


but that is coming back as null. How can I access the parent data through a child?

Thanks for any thoughts.


Top
 Profile  
 
 Post subject: Re: getting parent info from child
PostPosted: Sun Jan 24, 2010 3:56 am 
Newbie

Joined: Tue Jan 05, 2010 12:15 pm
Posts: 9
Can you post your classes as well as table an mapping


Top
 Profile  
 
 Post subject: Re: getting parent info from child
PostPosted: Mon Jan 25, 2010 9:43 am 
Newbie

Joined: Thu Dec 03, 2009 2:10 pm
Posts: 13
Here is my parent class -
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Data" namespace="Data">
  <class name="Data.Parent, Datatable="parent">
    <id name="field1_key">
      <generator class="identity"/>
    </id>
    <property name="field2"/>
    <property name="field3"/>   

    <set name="child" table="child" generic="true" inverse="true">
      <key column="ch_field1_key" />
      <one-to-many class="Data.child, Data"/>
    </set>
   
  </class> 
</hibernate-mapping>


Here is my parent class -
Code:
public class parent
    {
        virtual public int field1_key { get; set; }
        virtual public int field2 { get; set; }
        virtual public int field3 { get; set; }
     }


Here is my child mapping -
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Data" namespace="Data">
  <class name="Data.child, Data" table="child">
    <id name="ch_field1_key">
      <generator class="native"/>
    </id>
    <property name="field2"/>   
  </class>
</hibernate-mapping>


and here is my child class -
Code:
public class child
    {
        public virtual int ch_field1_key { get; set; }
        public virtual int field2 { get; set; }

        public virtual parent myParent { get; set; }
     }


Thanks


Top
 Profile  
 
 Post subject: Re: getting parent info from child
PostPosted: Tue Jan 26, 2010 9:00 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The mapping for the parent is missing in your child mapping:

<many-to-one name="myParent" ...../>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: getting parent info from child
PostPosted: Tue Jan 26, 2010 9:37 am 
Newbie

Joined: Thu Dec 03, 2009 2:10 pm
Posts: 13
I have tried adding that in, but then I get an error saying could not execute query - column is not found in any table in the query and it is referencing the key for the parent table.


Top
 Profile  
 
 Post subject: Re: getting parent info from child
PostPosted: Tue Jan 26, 2010 9:58 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You need a column for the parent id in your child table. It doesn't make sense to have a one-to many association from parent to child, when only one child can exist (you currently use the primary key of the child table).

Have a look at this for mapping parent/child relationships:

http://nhforge.org/doc/nh/en/index.html#example-parentchild

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: getting parent info from child
PostPosted: Tue Jan 26, 2010 10:10 am 
Newbie

Joined: Thu Dec 03, 2009 2:10 pm
Posts: 13
I was able to get it to work. in my child mapping, I was putting the parent key for the column value of the mapping -
Code:
<many-to-one name="Parent" column="parent-key" />


when it should have been set as -

Code:
<many-to-one name="Parent" column="child-key" />


Thanks for all the help.


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