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.  [ 5 posts ] 
Author Message
 Post subject: Mapping inherited objects from 2 separate tables
PostPosted: Tue Nov 27, 2007 4:23 pm 
Newbie

Joined: Fri Nov 09, 2007 10:01 am
Posts: 15
Dear NHibernate-user

I have Categories that has a Strongly Typed Collection of bsSubcategory. Each item in this collection could either be bsRegularSubcategory or bsPackage. Both inherited classes should always have an Id, PositionInCat, Title and Subtitle.

Code:
Public Class bsCategory
    Private mId As Integer
    Private mSubcategories As IList(Of bsSubcategory)
----------------------------------------------------
Public MustInherit Class bsSubcategory
    Private mId As Integer
    Private mPositionInCat As Integer
    Private mTitle As String
    Private mSubtitle As String
----------------------------------------------------
Public Class bsRegularSubcategory
    Inherits bsSubcategory
----------------------------------------------------
Public Class bsPackage
    Inherits bsSubcategory


The mapping is as following. At this moment I can attach a collection of bsRegularSubcategory OR a collection of bsPackage, but not both at the same time in one collection....preferrably ordered by PositionInCat throughout both inherited types.

My mapping files are as following (at this moment only items of bsRegularSubcategory are added to the collection):

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHibernateProject.bsCategory, NHibernateProject" table="TBL_CATEGORIES">
    <id name="Id" column="Cat_Id" type="Int32">
      <generator class="assigned" />
    </id>
    <property name="Name" column="Cat_Name" type="String" length="50"/>
    <property name="PositionInGrp" column="Cat_PositionInGrp" type="Int32"/>
    <bag name="Subcategories" order-by="SubCat_PositionInCat" table="TBL_SUBCATEGORIES">
      <key column="SubCat_Cat_Id" />
      <many-to-many class="NHibernateProject.bsRegularSubcategory, NHibernateProject" column="SubCat_Id"/>
    </bag>
  </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="NHibernateProject.bsRegularSubcategory, NHibernateProject" table="TBL_SUBCATEGORIES" polymorphism="implicit">
      <id name="Id" column="SubCat_Id" type="Int32">
        <generator class="assigned" />
      </id>
      <property name="PositionInCat" column="SubCat_PositionInCat" type="Int32"/>
      <property name="Title" column="SubCat_Title" type="String" length="50"/>
      <property name="Subtitle" column="SubCat_Subtitle" type="String" length="50"/>
    </class>
    <class name="NHibernateProject.bsPackage, NHibernateProject" table="TBL_PACKAGES" polymorphism="implicit">
      <id name="Id" column="Pac_Id" type="Int32">
        <generator class="assigned" />
      </id>
      <property name="PositionInCat" column="Pac_PositionInCat" type="Int32"/>
      <property name="Title" column="Pac_Title" type="String" length="50"/>
      <property name="Subtitle" column="Pac_Subtitle" type="String" length="50"/>
    </class>
</hibernate-mapping>


I made a screenshot of what resides in my database at this moment. Both tables TBL_PACKAGES and TBL-SUBCATEGORIES contain deviating columns, but showing this could only make it more confusing:

Image

The result should be as following, ordered within Category with Id = 205:
- Regular Subcategory-Id: 196
- Package-Id: 2
- Regular Subcategory-Id: 197

Could anyone help me out, please? I started to like NHibernate, but now I am stuck again.

Thanks in advance. Bennieke.


Last edited by bennieke on Tue Nov 27, 2007 4:38 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Mapping inherited objects from 2 separate tables
PostPosted: Tue Nov 27, 2007 4:36 pm 
Newbie

Joined: Fri Nov 09, 2007 10:01 am
Posts: 15
--------


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 12:59 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
When using table-per-concrete class, you have to use <many-to-any> to acheive a polymorphic collection. See http://www.hibernate.org/hib_docs/nhibe ... tance.html.

Not sure what method you could for the ordering. Maybe use a list with index?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 6:09 am 
Newbie

Joined: Fri Nov 09, 2007 10:01 am
Posts: 15
Marcal

Should I add this <many-to-any> to my mapping for bsRegularSubcategory and/or bsPackage? Or do I have to add this to mapping for bsCategory? The difficulty is that the Foreign Key-column of the Category, has different names in both tables. As you can see, there is no single reference in the Categories-table towards its Regular Subcategories or Packages.

Could you or anyone point me to a direction with the given case in code? I am turning around in circles when I try to find code for this. More experienced users could be more familiar with adapting the above mentioned mapping code??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 29, 2007 2:41 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Well, you've got me there. Frankly, in this case I was just trying to start you down the path with a docs reference. I don't have experience with this. However, after looking closer at the documentation, it doesn't seem like proceeding down this path is a good idea at all. I would highly recommend reworking your inheritance to table-per-hierarchy.

If that is not possible, perhaps you could add a Pac_Cat_Id colution to TBL_SUBCATEGORIES and use triggers to keep it synched with SubCat_Cat_Id? That's pretty ugly.

Or, you could map these as two separate protected or private collections on the Category, then expose an interleaved collection to the user. That would require a lot of customized ugliness internal to the class.

Sorry I'm not of more help.


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