-->
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: Inheritance Mapping
PostPosted: Wed Aug 08, 2007 5:54 am 
Newbie

Joined: Wed Aug 08, 2007 5:21 am
Posts: 3
Hibernate version:1.2.0.GA

Hi I'm mapping manually(Smart code wouldn't work with my old Oracle database)... and this is a mapping question.
I have chosen a "table per concrete class" strategy, to map to a List, as suggested in chapter 8 in the documentation.

So I have this List in a class -IntermediateTrack:
Code:
private List<IRailwayObject> m_RailwayObjects;

and I have 3 subobjects implementing my RailwayObject class. I have mapped and tested the subobject, they work fine. But how do I fill my list with random railway objects... I can't quiet capture the idea from the chapter. How do I use the
Code:
<any>
thing?

Do I have to map the base class -RailwayObject, even though it has no table?

I have three tables, which all have a Foreign key containing an ID from one other table. The foreign key column are named the same in all 3 tables -"IntermediateTrackId".

Mapping document for IntermediateTrack:


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="BDK.TIB.ObjectModel" namespace="BDK.TIB.ObjectModel">
<class name="BDK.TIB.ObjectModel.IntermediateTrack, BDK.TIB.ObjectModel" table="INTERMEDIATETRACK" lazy="false">
<id name="ID" column="ID">
<generator class="sequence">
<param name="sequence">SEQTIB</param>
</generator>
</id>
<many-to-one name="DepartureSectionStation" class="BDK.TIB.ObjectModel.SectionStation, BDK.TIB.ObjectModel" column="DEPARTURESTATIONID" />
<many-to-one name="DestinationSectionStation" class="BDK.TIB.ObjectModel.SectionStation, BDK.TIB.ObjectModel" column="DESTINATIONSTATIONID" />
<set name="RailwayObjects">
<key column="INTERMEDIATETRACKID" />
<many-to-many class="BDK.TIB.ObjectModel.RailwayObject, BDK.TIB.ObjectModel" />
</set>
</class>
</hibernate-mapping>



I hope u don't mind -I didn't care to make question more general, by changing my objects to cats and mice :D
Hope you guys can help me?
Sorry about my english...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 10, 2007 8:19 am 
Newbie

Joined: Wed Aug 08, 2007 5:21 am
Posts: 3
Still need help...

Changed the list type to:

Code:
PersistentGenericSet<IRailwayObject> m_RailwayObjects;


And tried the following, but any are not accepted...

Code:
   <set name="RailwayObjects">
         <key column="INTERMEDIATETRACKID" />
         <many-to-many class="BDK.TIB.ObjectModel.RailwayObjects, BDK.TIB.ObjectModel">
            <any name="RailwayObjects" meta-type="string" >
               <meta-value value="FC" class="FC"/>
               <meta-value value="STATIONRADIOCHANNEL" class="StationRadioChannel"/>
               <meta-value value="SPEED" class="Speed"/>
               <column name="ID"/>               
            </any>
         </many-to-many>         
      </set>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 5:12 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Change your collection type to IList<IRailwayObject> and the mapping type to <bag>.

Code:
<bag name="RailwayObjects">
<key column="INTERMEDIATETRACKID" />
<many-to-many class="BDK.TIB.ObjectModel.RailwayObject, BDK.TIB.ObjectModel" />
</bag>


<set> should be used if you want to use the Iesi.Collections set. <bag> is the IList<T> equivalent in .NET.
<map> is an IDictionary<K,V> equivalent.

Hope this helps...
Mike

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 10:30 am 
Newbie

Joined: Wed Aug 08, 2007 5:21 am
Posts: 3
Hi Mike.

Thank you for your answer, had given up on this forum...

Yes I normally use the bag and IList method, when I map a collection of objects... but was desperate, so I have tested a lot of options... So I'm actually back to the IList... -as u suggest...

But the problem is if I do as you describe, I'm referening to an abstract class Railwayobject, which is a base class with no table bound to it... So it has no mapping file like the subclasses...
-(The table-per-concrete-class stragey), So if I try your method I get the message:
Code:
{"An association from the table RailwayObjects refers to an unmapped class: RailwayObject"}


Should I map the abstract class???
Please help I'm about to give up on NHibernate...

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 11:10 am 
Newbie

Joined: Thu Oct 20, 2005 5:17 pm
Posts: 19
*edit*
Sorry, I didn't realize at first that you were using the table-per-concrete class strategy. Referring again to Hibernate in Action, it warns that using this mapping strategy makes it difficult to represent polymorphic associations, because you can't map a foreign key relationship to the table of the abstract superclass. Chapter 6 of the book gives an example of using the <any> element, though if it's not too late, I would highly recommend moving to a table-per-class strategy unless something is preventing you from doing so.

If you haven't picked up the Hibernate in Action book, I would recommend doing so before giving up. It's an invaluable reference.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 12:09 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Jeff is right on with this...I'm afraid I glossed over your choice of the table-per-concrete-class strategy. See here
http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html_single/#inheritance-limitations

Is this db schema something you can't control (legacy)? If you can, then I'd recommend choosing a different strategy to simplify things and open up the query options/association options for your model.

_________________
If this helped...please remember to 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.