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 Movement As Char Implements
IItinerary.Movement
        Get
            Return "I"
        End Get
        'Set(value As Boolean)
        '    value = True
        'End Set
    End Property

    Public Overridable Property ModifiedDate() As Nullable(Of
DateTime) Implements IItinerary.ModifiedDate
        Get
            Return _modifiedDate
        End Get
        Set(value As Nullable(Of DateTime))
            _modifiedDate = value
        End Set
    End Property

    Public Overridable Property Personnel As Personnel Implements
IItinerary.Personnel
        Get
            Return _personnel
        End Get
        Set(ByVal value As Personnel)
            _personnel = value
        End Set
    End Property

    Public Overrides Function CompareTo(ByVal obj As Object) As
Integer
        Dim objArrivalDate As DateTime = Nothing

        If TypeOf (obj) Is InboundItinerary Then
            With CType(obj, InboundItinerary)
                objArrivalDate = .ArrivalTime.GetValueOrDefault
            End With
        End If

        Return Date.Compare(Me.ArrivalTime.GetValueOrDefault,
objArrivalDate)
    End Function
End Class
------------------------------------------------------

-- 
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.

Reply via email to