-->
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.  [ 8 posts ] 
Author Message
 Post subject: Bidirectional Child when there are many Parent classes
PostPosted: Wed Jul 02, 2008 4:01 pm 
Newbie

Joined: Fri Jun 06, 2008 6:25 pm
Posts: 8
I have the situation were we have many Parent classes that have a one-to-many relationship to a common Child class. I would like to have the child be bidirectional; i.e. the Child class having a reference back to the parent.

I have it working for a single Parent/Child relationship, but I am unsure how to map it when there are mulitple Parent class types from the Childs perspective.

In general, how do I do the mapping for the Child?
Are there any changes I need to make for the standard Parent mapping?


ParentClassA => (1-n) => Child
ParentClassB => (1-n) => Child

Code:
public class ParentClassA : ParentBase
{
    private IList<Child> m_children;
...
    public IList<Child> Children
    {
        get { return m_children; }
        set { m_children = value }
    }
}

public class ParentClassB : ParentBase
{
    private IList<Child> m_children;
...
    public IList<Child> Children
    {
        get { return m_children; }
        set { m_children = value }
    }
}

public class Child
{
    private ParentBase m_parent;
...
    public ParentBase Parent
    {
        get { return m_parent; }
        set { m_parent= value }
    }
}


Please help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 3:11 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
ParentBase is the base class of ParentA and ParentB ? I would say, the children have to be defined there:

Code:
public class ParentBase
{
    private IList<Child> m_children;
...
    public IList<Child> Children
    {
        get { return m_children; }
        set { m_children = value }
    }
}

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 9:17 am 
Newbie

Joined: Fri Jun 06, 2008 6:25 pm
Posts: 8
I agree, but what does that mean for the mapping files?
Can ParentBase be an abstract class?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 9:34 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Yes, it can. have a look at the different strategies for inheritance mapping:

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/inheritance.html

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 12:54 pm 
Newbie

Joined: Fri Jun 06, 2008 6:25 pm
Posts: 8
You seem to have pointed me to how to persist & hydrate a Parent. In our implementation nearly all our classes inherit from the same abstract base class (all 50 of them). The question was not how to polymorphically create ParentA and ParentB from the same table using discriminators, etc. ParentA and ParentB are from different tables with very different content and purposes... but they all have one-to-many reference to a common child table.

Let's say ParentA is a Customer, ParentB is an InvoiceLineItem, and the Child is a generic DiscriptiveFeatures table.

Code:
public abstract class ParentBase
{
    private IList<DiscriptiveFeature> m_features;
...
    public IList<DiscriptiveFeature> Features
    {
        get { return m_features; }
        set { m_features= value }
    }
}

public class DiscriptiveFeature
{
    private ParentBase m_parent;

    public int FeatureType {...}
    public bool Shown {...}
    public int SecurityLevel {...}
    ...
    public ParentBase Parent
    {
        get { return m_parent; }
        set { m_parent= value }
    }
}

public class Customer : ParentBase
{
    ... // class specific properties
}

public class InvoiceLineItem: ParentBase
{
    ... // class specific properties
}



The original question was: "How do I bidirectionaly map the DiscriptiveFeatures/child?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 1:05 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
One of these strategies is "table-per-subclass" which seems to be the one you are using ? You should find some information there on how to map your case. That's why I posted that link.

Would be something like this (only the relevant stuff):


Code:
<class name="FeaturedItem" ...>
...
   <many-to-one name="Parent" column="parentid" not-null="true" />
...
</class>

<class name="Customer">
...
  <bag name="Features" ... inverse="true">
      <key column="parentid"/>
      <one-to-many class="FeaturedItem"/>
  </bag>
...
</class>

<class name="InvoiceLineItem">
...
  <bag name="Features" ... inverse="true">
      <key column="parentid"/>
      <one-to-many class="FeaturedItem"/>
  </bag>
...
</class>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 4:15 pm 
Newbie

Joined: Fri Jun 06, 2008 6:25 pm
Posts: 8
I did what you suggested, but now I get the error:
"An association from the table DescriptiveFeatures refers to an unmapped class: Framework.Model.Core.ParentBase".

How can I map an abstract class?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 08, 2008 2:25 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not really sure, I think you need a "any" mapping for that. Checkout the last part of the chapter I posted before. And have a look here:

http://www.ayende.com/Blog/archive/2006/06/05/7532.aspx

_________________
--Wolfgang


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