Thanks for the response Marcelo! Ok, I have tried making the changes you suggested. I suppose you could say that putting the properties into the subclasses was by design, but I couldn't find any good information on how to implement this scenario in the NH documentation. My parent class is just an interface. I wasn't sure if I should use the "abstract=true" property for its tag either. It's not a true abstract class, but it's not a concrete class either.
When I try to put the common properties in the parent class (IItinerary) in the mapping, I get the following error: "The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'discriminator' in namespace 'urn:nhibernate- mapping-2.2'. List of possible elements expected: 'property, many-to- one, one-to-one, component, dynamic-component, properties, any, map, set, list, bag, idbag, array, primitive-array, join, subclass, joined- subclass, union-subclass, loader, sql-insert, sql-update, sql-delete, filter, resultset, query, sql-query' in namespace 'urn:nhibernate- mapping-2.2'." Leaving the properties in the subclasses, and making the Movement field non-nullable doesn't fix the issue. I still get the same error: "Object with id: 4 was not of the specified subclass: MyApp.DomainEntities.InboundItinerary (loading object was of wrong class [MyApp.DomainEntities.OutboundItinerary])" Is there anything else that looks wrong in my configuration? On Apr 20, 3:22 pm, Marcelo Grossi <[email protected]> wrote: > Movement field should not be nullable. And also you have repeated > properties in your subclasses, is this by design? You should place the > common properties in the parent class.. it shouldn't matter if your > subclasses have no properties. > > -- > Marcelo Grossi > > On Apr 20, 4:23 am, Arthur Dent <[email protected]> wrote: > > > > > > > > > I'm trying to implement the table per class hierarchy model for > > itineraries in my app. I have a single Itinerary table, and the > > Movement field will have either an "I" or "O" as the discriminator for > > "inbound" or "outbound" itineraries. > > > I have set up an "IItinerary" interface to use as my parent class. > > Both InboundItinerary and OutboundItinerary classes implement this. > > They also inherit from a base Itinerary class, that in turn inherits > > from a BaseObject class that I use for most of the objects in my app. > > > The problem I'm having is that it doesn't seem to be able to determine > > what type of itinerary each record is when they get loaded. I get > > this error: > > "Object with id: 1 was not of the specified subclass: > > MyApp.DomainEntities.InboundItinerary (loading object was of wrong > > class [MyApp.DomainEntities.OutboundItinerary])" > > > Am I doing something wrong in my implementation? I found some similar > > issues while searching for an answer, and I turned off lazy loading > > because that seemed to fix it for some people. Is there something > > else I'm missing? > > > Itinerary Table > > ------------------------------------------------------ > > CREATE TABLE [dbo].[Itinerary]( > > [Id] [int] IDENTITY(1,1) NOT NULL, > > [EnrollmentId] [int] NOT NULL, > > [PersonnelId] [int] NULL, > > [AirlineId] [smallint] NOT NULL, > > [Movement] [char](1) NULL, > > [FlightNumber] [varchar](25) NOT NULL, > > [DepartureLocation] [varchar](25) NOT NULL, > > [DepartureTime] [smalldatetime] NOT NULL, > > [ArrivalLocation] [varchar](25) NOT NULL, > > [ArrivalTime] [smalldatetime] NOT NULL, > > [SpecialInstructions] [varchar](max) NULL CONSTRAINT > > [DF_Itinerary_Instructions] DEFAULT (space((0))), > > [ModifiedDate] [smalldatetime] NULL CONSTRAINT [DF_Itinerary_Date] > > DEFAULT (getdate()), > > CONSTRAINT [PK_Itinerary] PRIMARY KEY CLUSTERED > > ( > > [Id] ASC > > )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY > > = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] > > ) ON [PRIMARY] > > ------------------------------------------------------ > > > Itinerary Mapping > > ------------------------------------------------------ > > <?xml version="1.0" encoding="utf-8" ?> > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" > > assembly="MyApp.DomainEntities, Version=1.0.0.0, Culture=neutral, > > PublicKeyToken=01ca2278eac9ed50" namespace="MyApp.DomainEntities" > > default-lazy="false"> > > > <class name="IItinerary" table="Itinerary" abstract="true" > > discriminator-value="A"> > > <id name="Id" column="Id" type="Int32"> > > <generator class="native" /> > > </id> > > > <discriminator column="Movement" type="Char" /> > > <subclass name="InboundItinerary" discriminator-value="I" > > lazy="false"> > > <many-to-one name="Personnel" column="PersonnelId" > > class="Personnel"/> > > <many-to-one name="ClassEnrollment" column="EnrollmentId" > > class="ClassEnrollment"/> > > <many-to-one name="Airline" column="AirlineId" class="Airline"/> > > > <property name="FlightNumber" type="String" /> > > <property name="DepartureLocation" type="String" /> > > <property name="DepartureTime" type="DateTime" /> > > <property name="ArrivalLocation" type="String" /> > > <property name="ArrivalTime" type="DateTime" /> > > <property name="Instructions" column="SpecialInstructions" > > type="String" /> > > <property name="ModifiedDate" type="DateTime" /> > > </subclass> > > <subclass name="OutboundItinerary" discriminator-value="O" > > lazy="false"> > > <many-to-one name="Personnel" column="PersonnelId" > > class="Personnel"/> > > <many-to-one name="ClassEnrollment" column="EnrollmentId" > > class="ClassEnrollment"/> > > <many-to-one name="Airline" column="AirlineId" class="Airline"/> > > > <property name="FlightNumber" type="String" /> > > <property name="DepartureLocation" type="String" /> > > <property name="DepartureTime" type="DateTime" /> > > <property name="ArrivalLocation" type="String" /> > > <property name="ArrivalTime" type="DateTime" /> > > <property name="Instructions" column="SpecialInstructions" > > type="String" /> > > <property name="ModifiedDate" type="DateTime" /> > > </subclass> > > > </class> > > </hibernate-mapping> > > ------------------------------------------------------ > > > IItinerary Interface > > ------------------------------------------------------ > > Public Interface IItinerary > > Property Airline() As Airline > > Property ArrivalLocation() As String > > Property ArrivalTime() As Nullable(Of DateTime) > > Property ClassEnrollment() As ClassEnrollment > > Property Personnel As Personnel > > ReadOnly Property Movement As Char > > Property ModifiedDate() As Nullable(Of DateTime) > > Property DepartureLocation() As String > > Property DepartureTime() As Nullable(Of DateTime) > > Property FlightNumber() As String > > Property Id() As Integer > > Property Instructions() As String > > End Interface > > ------------------------------------------------------ > > > Itinerary Base Class > > ------------------------------------------------------ > > <Serializable()> _ > > Public MustInherit Class Itinerary > > Inherits BaseObject > > Implements IComparable > > > Public MustOverride Function CompareTo(ByVal obj As Object) As > > Integer Implements System.IComparable.CompareTo > > > End Class > > ------------------------------------------------------ > > > InboundItinerary Class (Outbound is essentially identical aside from > > the name) > > ------------------------------------------------------ > > <Serializable()> _ > > Public Class InboundItinerary > > Inherits Itinerary > > Implements IComparable > > Implements IItinerary > > > Private _airline As Airline > > Private _arrivalLocation As String > > Private _arrivalTime As Nullable(Of DateTime) > > Private _departureLocation As String > > Private _departureTime As Nullable(Of DateTime) > > Private _enrollment As ClassEnrollment > > Private _flightNumber As String > > Private _id As Integer > > Private _instructions As String > > Private _movement As Char > > Private _modifiedDate As Nullable(Of DateTime) > > Private _personnel As Personnel > > > Public Overridable Property Airline() As Airline Implements > > IItinerary.Airline > > Get > > Return _airline > > End Get > > Set(ByVal value As Airline) > > _airline = value > > End Set > > End Property > > > Public Overridable Property ArrivalLocation() As String Implements > > IItinerary.ArrivalLocation > > Get > > Return _arrivalLocation > > End Get > > Set(ByVal value As String) > > _arrivalLocation = value > > End Set > > End Property > > > Public Overridable Property ArrivalTime() As Nullable(Of DateTime) > > Implements IItinerary.ArrivalTime > > Get > > Return _arrivalTime > > End Get > > Set(ByVal value As Nullable(Of DateTime)) > > _arrivalTime = value > > End Set > > End Property > > > Public Overridable Property ClassEnrollment() As ClassEnrollment > > Implements IItinerary.ClassEnrollment > > Get > > Return _enrollment > > End Get > > Set(ByVal value As ClassEnrollment) > > _enrollment = value > > End Set > > End Property > > > Public Overridable Property DepartureLocation() As String > > Implements IItinerary.DepartureLocation > > Get > > Return _departureLocation > > End Get > > Set(ByVal value As String) > > _departureLocation = value > > End Set > > End Property > > > Public Overridable Property DepartureTime() As Nullable(Of > > DateTime) Implements IItinerary.DepartureTime > > Get > > Return _departureTime > > End Get > > Set(ByVal value As Nullable(Of DateTime)) > > _departureTime = value > > End Set > > End Property > > > Public Overridable Property FlightNumber() As String Implements > > IItinerary.FlightNumber > > Get > > Return _flightNumber > > End Get > > Set(ByVal value As String) > > _flightNumber = value > > End Set > > End Property > > > Public Overridable Property Id() As Integer Implements > > IItinerary.Id > > Get > > Return _id > > End Get > > Set(ByVal value As Integer) > > _id = value > > End Set > > End Property > > > Public Overridable Property Instructions() As String Implements > > IItinerary.Instructions > > Get > > Return _instructions > > End Get > > Set(ByVal value As String) > > _instructions = value > > End Set > > End Property > > > Public Overridable ReadOnly Property > > ... > > read more » -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
