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.  [ 13 posts ] 
Author Message
 Post subject: System.InvalidCastException: Unable to cast object of type..
PostPosted: Thu Sep 21, 2006 11:04 am 
Newbie

Joined: Wed Sep 20, 2006 5:03 pm
Posts: 10
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

I have a strange casting problem that I've beat my head on for days and cannot seem to figure out. Below is my test case and code postings. The issue is when trying to cast from LOCATION to ShortTimeLocation, as you'll discover in the error message. These objects inherit from one another using the table-per-class pattern. The database is a legacy database, not very well normalized, and I thus CANNOT change the database structure. So I built the object data model to fit the relational data model as best I could. No matter what I do, I cannot seem to satify NHibernate so that it will handle the casting correctly. I expected it not to be a problem as per rules of polymorphism, since the objects directly related via inheritence. Am I missing something?

[UPDATE:] I should mention also for clarity, that the code in the test case is attempting to do a QBE via the ICriteria facility in NHibernate. It fails during the ICriteria.List<LongCrash>() call.

However, if the test case PASSED when I did the HQL route using IQuery. For example, this code FAILS:

Code:
IList<LongCrash> results = criteria.List<LongCrash>();


But this code SUCCEEDS:

Code:
NHibernate.IQuery query = session.CreateQuery( "select c from Crash as c" );


Thanks in advance for your help, guys.

Hibernate version: NHibernate 1.2.0.1001 (1.2.0.Alpha1)

Below are my POCOs. Note that I used NHibernate.Mapping.Attributes. Also note that I'm using proxies and interfaces to provide some abstraction from the ugly legacy relational data model. Specifically, observe how I set up the ILocation, LOCATION, and ShortTimeLocation objects since they are the ones giving me the problems.

Code:
public delegate void ObjectChangingEventHandler( object sender, System.EventArgs args );

public interface IObjectState : System.ComponentModel.INotifyPropertyChanged {
  bool Dirty {
    get;
  }
}

public abstract partial class ObjectState : IObjectState {
  private bool _dirty = false;

  public virtual event ObjectChangingEventHandler ObjectChanging;
  public virtual event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

  public bool Dirty {
    get {
      return _dirty;
    }
    protected set {
      if ( _dirty != value ) {
        _dirty = value;       
      }
    }
  }

  #region Methods
  protected virtual void OnChanging() {
    if ( ( this.ObjectChanging != null ) ) {
      this.ObjectChanging( this, System.EventArgs.Empty );
    }
  }

  protected virtual void OnChanging( string property ) {
    if ( ( this.ObjectChanging != null ) ) {
      this.ObjectChanging( this, System.EventArgs.Empty );
    }
  }

  protected virtual void OnChanged( string property ) {
    Dirty = true;
    if ( this.PropertyChanged != null ) {
      this.PropertyChanged( this, new System.ComponentModel.PropertyChangedEventArgs( property ) );
    }
  }

  protected virtual void OnChanged() {
    Dirty = true;
    if ( this.PropertyChanged != null ) {
      this.PropertyChanged( this, (System.ComponentModel.PropertyChangedEventArgs) System.ComponentModel.PropertyChangedEventArgs.Empty );
      //this.PropertyChanged( this, new System.ComponentModel.PropertyChangedEventArgs( "<Not Reported>" ) );
    }
  }
  #endregion
}

public interface IProduct : IObjectState {
  string CaseNumber {
    get;
    set;
  }
  string Id {
    get;
    set;
  }
  string ProductName {
    get;
    set;
  }
}

public interface ICrash : IProduct {
  IContributingCauses ContributingCauses {
    get;
    set;
  }

  ILocation Location {
    get;
    set;
  }

  IVehiclePedestrian VehiclePedestrians {
    get;
    set;
  }
}

public interface ILocation : IObjectState {
  string Id {
    get;
    set;
  }

  string Latitude {
    get;
    set;
  }
  string Longitude {
    get;
    set;
  }
}

public interface IContributingCauses : IObjectState {
  string CauseEnvironment1 {
    get;
    set;
  }
  string Id {
    get;
    set;
  }
}

public interface IVehiclePedestrian : IObjectState {
  string EstDamage1 {
    get;
    set;
  }
  string Id {
    get;
    set;
  }
  IProduct Product {
    get;
    set;
  }
}

[Class( NameType = typeof( ProductHeader ), Polymorphism = PolymorphismType.Explicit, ProxyType = typeof( IProduct )
  , Table = "`Prd_Header`", Mutable = false, Lazy = false
, DynamicUpdate = false, DynamicInsert = false )]
public partial class ProductHeader : ObjectState, IProduct {
  #region Fields
  private string _id;
  private string _caseNumber;
  private string _productName;
  #endregion

  #region Ctors
  public ProductHeader() {
  }
  #endregion

  #region Properties

  [Id( 0, Name = "Id", Length = 100, Access = "property", TypeType = typeof( string ) )]
  [Column( 1, Name = "`PrdKey`", Length = 100, Unique = true, NotNull = true )]
  [Generator( 2, Class = "native" )]
  public virtual string Id {
    get {
      return this._id;
    }
    set {
      this._id = value;
    }
  }

  #region Other Properties
  [Property( Name = "CaseNumber", Column = "`CaseNumber`"
    , Access = "property", NotNull = false
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public virtual string CaseNumber {
    get {
      return this._caseNumber;
    }
    set {
      this._caseNumber = value;
    }
  }

  [Discriminator( 0, Column = "`PrdName`" )]
  [Property( 1, Name = "ProductName", Column = "`PrdName`", Length = 100, Access = "property"
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public virtual string ProductName {
    get {
      return this._productName;
    }
    set {
      this._productName = value;
    }
  }
  #endregion
  #endregion

  #region Methods
  public override string ToString() {
    return this.CaseNumber != null ? this.CaseNumber.ToString() : "<null>";
  }
  #endregion
}

[Subclass( DiscriminatorValue = "base", NameType = typeof( Crash ), ExtendsType = typeof( ProductHeader )
         , ProxyType = typeof( ICrash ), Lazy = false
         , DynamicUpdate = false, DynamicInsert = false )]
public partial class Crash : ProductHeader, ICrash, IProduct {
  #region Fields
  [b]private ILocation _location =new LOCATION();[/b]
  private IContributingCauses _contributingCauses =new ContributingCauses();
  private IVehiclePedestrian _vehiclePedestrians =new VehiclePedestrian();
  #endregion

  #region Ctors
  public Crash() {
  }
  #endregion

  #region Properties
    public virtual ILocation Location {
    get {
      return _location;
    }
    set {
      if ( _location != value ) {
        OnChanging( "Location" );
        _location = value;
        OnChanged( "Location" );
      }
    }
  }

  public virtual IContributingCauses ContributingCauses {
    get {
      return _contributingCauses;
    }
    set {
      _contributingCauses = value;
    }
  }

  public virtual IVehiclePedestrian VehiclePedestrians {
    get {
      return _vehiclePedestrians;
    }
    set {
      _vehiclePedestrians = value;
    }
  }
  #endregion
}

public partial class ContributingCauses : ObjectState, IContributingCauses {
  #region Fields
  private string _id;
  private string _CauseEnvironment1;
  private IProduct _product =new ProductHeader();
  #endregion

  #region Ctors
  public ContributingCauses() {
  }
  #endregion

  #region Properties

  public virtual string Id {
    get {
      return _id;
    }
    set {
      _id = value;
    }
  }

  public virtual IProduct Product {
    get {
      return _product;
    }
    set {
      _product = value;
    }
  }

  public virtual string CauseEnvironment1 {
    get {
      return this._CauseEnvironment1;
    }
    set {
      if ( ( this._CauseEnvironment1 != value ) ) {
        this.OnChanging();
        this._CauseEnvironment1 = value;
        this.OnChanged( @"CauseEnvironment1" );
      }
    }
  }
  #endregion

  #region Methods
  public override string ToString() {
    return this._id != null ? this._id.ToString() : "";
  }
  #endregion
}

public partial class VehiclePedestrian : ObjectState, IVehiclePedestrian {
  #region Fields
  private string _id;
  private string _EstDamage1;
  private IProduct _product =new ProductHeader();
  #endregion

  #region Ctors
  public VehiclePedestrian() {
  }
  #endregion

  #region Properties

  public virtual string Id {
    get {
      return _id;
    }
    set {
      _id = value;
    }
  }

  public virtual IProduct Product {
    get {
      return _product;
    }
    set {
      _product = value;
    }
  }

  public virtual string EstDamage1 {
    get {
      return this._EstDamage1;
    }
    set {
      if ( ( this._EstDamage1 != value ) ) {
        this.OnChanging();
        this._EstDamage1 = value;
        this.OnChanged( "EstDamage1" );
      }
    }
  }
  #endregion

  #region Methods

  public override string ToString() {
    return this._id != null ? this._id.ToString() : "";
  }

  #endregion
}

[Subclass( DiscriminatorValue = "CRASH-LONG", NameType = typeof( LongCrash ), ProxyType = typeof( ICrash )
  , ExtendsType = typeof( Crash ), Lazy = false, DynamicUpdate = false, DynamicInsert = false )]
public partial class LongCrash : Crash, ICrash, IProduct {
  #region Fields
  private LongPaperPassenger _passengers =new LongPaperPassenger();
  private LongPaperWitnesses _witnesses =new LongPaperWitnesses();
  #endregion

  #region Ctors
  public LongCrash() {
  }
  #endregion

  #region Properties

  [Id( 0, Name = "Id", TypeType = typeof( string ), Length = 100 )]
  [Column( 1, Name = "`PrdKey`", NotNull = false, Unique = true, Length = 100 )]
  [Generator( 2, Class = "native" )]
  public new virtual string Id {
    get {
      return base.Id;
    }
    set {
      base.Id = value;
    }
  }

  OneToOne( 1, Name = "Location", ClassType = typeof( LongPaperTimeLocation ), PropertyRef = "Product"
   , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, Cascade = CascadeStyle.None )]
  public new virtual ILocation Location {
    get {
      return base.Location;
    }
    set {
      if ( base.Location != value ) {
        OnChanging( "Location" );
        base.Location = value;
        OnChanged( "Location" );
      }
    }
  }

  [OneToOne( 1, Name = "ContributingCauses", ClassType = typeof( LongPaperContributingCauses ), PropertyRef = "Product"
  , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, Cascade = CascadeStyle.None )]
  public override IContributingCauses ContributingCauses {
    get {
      return base.ContributingCauses;
    }
    set {
      base.ContributingCauses = value;
    }
  }

  [OneToOne( 1, Name = "VehiclePedestrians", ClassType = typeof( LongPaperVehiclePedestrian ), PropertyRef = "Product"
  , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, Cascade = CascadeStyle.None )]
  public override IVehiclePedestrian VehiclePedestrians {
    get {
      return base.VehiclePedestrians;
    }
    set {
      base.VehiclePedestrians = value;
    }
  }

  [OneToOne( 1, Name = "Passengers", ClassType = typeof( LongPaperPassenger ), PropertyRef = "Product"
  , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, Cascade = CascadeStyle.None )]
  public virtual LongPaperPassenger Passengers {
    get {
      return _passengers;
    }
    set {
      _passengers = value;
    }
  }

  [OneToOne( 1, Name = "Witnesses", ClassType = typeof( LongPaperWitnesses ), PropertyRef = "Product"
  , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, Cascade = CascadeStyle.None )]
  public virtual LongPaperWitnesses Witnesses {
    get {
      return _witnesses;
    }
    set {
      _witnesses = value;
    }
  }
  #endregion

  #region Methods
  public override string ToString() {
    return base.ToString();
  }
  #endregion
}

[Subclass( DiscriminatorValue = "ShortCrash", NameType = typeof( ShortCrash ), ProxyType = typeof( ICrash )
  , ExtendsType = typeof( Crash ), Lazy = false, DynamicUpdate = false, DynamicInsert = false )]
public partial class ShortCrash : Crash, ICrash, IProduct {
  #region Ctors
  public ShortCrash() {
  }
  #endregion

  #region Properties
  [Id( 0, Name = "Id", Length = 100, TypeType = typeof( string ) )]
  [Column( 1, Name = "`PrdKey`", NotNull = false, Unique = true, Length = 100 )]
  [Generator( 2, Class = "native" )]
  public new virtual string Id {
    get {
      return base.Id;
    }
    set {
      base.Id = value;
    }
  }

  [OneToOne( 1, Name = "Location", ClassType = typeof( LongPaperTimeLocation ), PropertyRef = "Product"
  , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, Cascade = CascadeStyle.None )]
  public new virtual ILocation Location {
    get {
      return base.Location;
    }
    set {
      if ( base.Location != value ) {
        OnChanging( "Location" );
        base.Location = value;
        OnChanged( "Location" );
      }
    }
  }

  [OneToOne( 1, Name = "ContributingCauses", ClassType = typeof( ShortContributingCauses ), PropertyRef = "Product"
  , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, Cascade = CascadeStyle.None )]
  public override IContributingCauses ContributingCauses {
    get {
      return base.ContributingCauses;
    }
    set {
      base.ContributingCauses = value;
    }
  }

  [OneToOne( 1, Name = "VehiclePedestrians", ClassType = typeof( ShortVehiclePedestrian ), PropertyRef = "Product"
  , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, Cascade = CascadeStyle.None )]
  public override IVehiclePedestrian VehiclePedestrians {
    get {
      return base.VehiclePedestrians;
    }
    set {
      base.VehiclePedestrians = value;
    }
  }
  #endregion
}

[Class( DiscriminatorValue = "LOCATION", NameType = typeof( LOCATION ), ProxyType = typeof( ILocation ), Table = "`LOCATION`"
, Polymorphism = PolymorphismType.Explicit, Mutable = false, Lazy = false
, DynamicUpdate = false, DynamicInsert = false )]
public partial class LOCATION : ObjectState, ILocation {
  #region Fields
  private string _id;
  private string _Latitude;
  private string _Longitude;
  #endregion

  #region Ctors
  public LOCATION() {
  }
  #endregion

  #region Properties

  [Id( 1, Name = "Id", Length = 100, Access = "property", TypeType = typeof( string ) )]
  [Column( 2, Name = "`COLLKEY`", Length = 100, Unique = true )]
  [Generator( 3, Class = "native" )]
  public virtual string Id {
    get {
      return this._id;
    }
    set {
      if ( ( this._id != value ) ) {
        this.OnChanging();
        this._id = value;
        this.OnChanged( "Id" );
      }
    }
  }

  #region Other Properties
  [Property( Name = "Latitude", Column = "`Latitude`", Access = "property", NotNull = false
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public virtual string Latitude {
    get {
      return this._Latitude;
    }
    set {
      if ( ( this._Latitude != value ) ) {
        this.OnChanging();
        this._Latitude = value;
        this.OnChanged( "Latitude" );
      }
    }
  }

  [Property( Name = "Longitude", Column = "`Longitude`", Access = "property", NotNull = false
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public virtual string Longitude {
    get {
      return this._Longitude;
    }
    set {
      if ( ( this._Longitude != value ) ) {
        this.OnChanging();
        this._Longitude = value;
        this.OnChanged( "Longitude" );
      }
    }
  }
  #endregion
  #endregion

  #region Methods
  public override string ToString() {
    return this.CityTown != null ? this.CityTown.ToString() : "<null>";
  }
  #endregion
}

[JoinedSubclass( NameType = typeof( LongPaperTimeLocation ), ExtendsType = typeof( LOCATION ), ProxyType = typeof( ILocation )
  , Table = "`LongPaperTimeLocation`", Lazy = false, DynamicUpdate = false, DynamicInsert = false )]
public partial class LongPaperTimeLocation : LOCATION, ILocation {
  #region Fields
  private string _agencyReportNumber;
  private IProduct _product =new ProductHeader();
  #endregion

  #region Ctors
  public LongPaperTimeLocation() {
  }
  #endregion

  #region Properties
  [Key( 0, Column = "`LocationColKey`" )]
  [Generator( 1, Class = "native" )]
  public override string Id {
    get {
      return base.Id;
    }
    set {
      base.Id = value;
    }
  }

  [ManyToOne( 1, Name = "Product", ClassType = typeof( LongCrash ), Column = "`PrdKey`", Unique = true
    , Update = false, Insert = false, Cascade = CascadeStyle.None
   , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, NotNull = true )]
  public virtual IProduct Product {
    get {
      return this._product;
    }
    set {
      this._product = value;
    }
  }

  #region Other Properties
  [Property( Name = "AgencyReportNumber", Formula = "`AgencyReportNumbert`", TypeType = typeof( string ), Insert = false, Update = false
           , Access = "property", NotNull = false )]
  public virtual string AgencyReportNumber {
    get {
      return this._agencyReportNumber;
    }
    set {
      this._agencyReportNumber = value;
    }
  }
  #endregion
  #endregion

  #region Methods
  public override string ToString() {
    return base.ToString();
  }
  #endregion
}

[Class( NameType = typeof( LongPaperContributingCauses ), ProxyType = typeof( IContributingCauses ), Table = "`LongPaperContributingCauses`"
, Polymorphism = PolymorphismType.Explicit, Mutable = false, Lazy = false
, DynamicUpdate = false, DynamicInsert = false, OptimisticLock = OptimisticLockMode.None )]
public partial class LongPaperContributingCauses : ContributingCauses, IContributingCauses {
  #region Ctors
  public LongPaperContributingCauses() {
  }
  #endregion

  #region Properties
  [Id( 0, Name = "Id", Length = 100, Access = "property", TypeType = typeof( string ) )]
  [Column( 1, Name = "`PrdKey`", Length = 100, Unique = true, NotNull = true )]
  [Generator( 2, Class = "native" )]
  public override string Id {
    get {
      return base.Id;
    }
    set {
      base.Id = value;
    }
  }

  [ManyToOne( 1, Name = "Product", ClassType = typeof( LongCrash ), Column = "`PrdKey`", Unique = true
   , Update = false, Insert = false, Cascade = CascadeStyle.None
    , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto, NotNull = true )]
  public override IProduct Product {
    get {
      return base.Product;
    }
    set {
      base.Product = value;
    }
  }

  #region Other Properties
  [Property( Name = "CauseEnvironment1", Formula = "`CauseEnvironment1`", Access = "property"
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public override string CauseEnvironment1 {
    get {
      return base.CauseEnvironment1;
    }
    set {
      if ( ( base.CauseEnvironment1 != value ) ) {
        base.CauseEnvironment1 = value;
      }
    }
  }
  #endregion
  #endregion

  #region Methods
  public override string ToString() {
    return Id != null ? Id.ToString() : "<null>";
  }
  #endregion
}

[Class( NameType = typeof( LongPaperVehiclePedestrian ), ProxyType = typeof( IVehiclePedestrian ), Table = "`LongPaperVehiclePedestrian`"
  , Polymorphism = PolymorphismType.Explicit, Mutable = false, Lazy = false
  , DynamicUpdate = false, DynamicInsert = false, OptimisticLock = OptimisticLockMode.None )]
public partial class LongPaperVehiclePedestrian : VehiclePedestrian, IVehiclePedestrian {
  #region Fields
  private string _LicState;
  #endregion

  #region Ctors
  public LongPaperVehiclePedestrian() {
  }
  #endregion

  #region Properties

  [Id( 0, Name = "Id", Length = 100, Access = "property", TypeType = typeof( string ) )]
  [Column( 1, Name = "`PrdKey`", Length = 100, Unique = true, NotNull = true )]
  [Generator( 2, Class = "native" )]
  public override string Id {
    get {
      return base.Id;
    }
    set {
      base.Id = value;
    }
  }

  [ManyToOne( 1, Name = "Product", ClassType = typeof( LongCrash ), Column = "`PrdKey`", Unique = true, NotNull = true
   , Update = false, Insert = false, Cascade = CascadeStyle.None
   , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto )]
  public override IProduct Product {
    get {
      return base.Product;
    }
    set {
      base.Product = value;
    }
  }

  #region Other Properties
  [Property( Name = "LicState", Formula = "`LicState`", Access = "property"
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public virtual string LicState {
    get {
      return this._LicState;
    }
    set {
      if ( ( this._LicState != value ) ) {
        this._LicState = value;
      }
    }
  }

  [Property( Name = "EstDamage1", Formula = "`EstDamage1`", Access = "property"
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public override string EstDamage1 {
    get {
      return base.EstDamage1;
    }
    set {
      if ( ( base.EstDamage1 != value ) ) {
        base.EstDamage1 = value;
      }
    }
  }
  #endregion
  #endregion

  #region Methods
  public override string ToString() {
    return Id != null ? Id.ToString() : "<null>";
  }
  #endregion
}

[Class( NameType = typeof( LongPaperPassenger ), Table = "`LongPaperPassengers`"
  , Polymorphism = PolymorphismType.Explicit, Mutable = false, Lazy = false
  , DynamicUpdate = false, DynamicInsert = false, OptimisticLock = OptimisticLockMode.None )]
public partial class LongPaperPassenger : ObjectState {
  #region Fields
  private string _id;
  private string _PassengerName1;
  private IProduct _product =new ProductHeader();
  #endregion

  #region Ctors
  public LongPaperPassenger() {
  }
  #endregion

  #region Properties

  [Id( 0, Name = "Id", Length = 100, Access = "property", TypeType = typeof( string ) )]
  [Column( 1, Name = "`PrdKey`", Length = 100, Unique = true )]
  [Generator( 2, Class = "native" )]
  public virtual string Id {
    get {
      return _id;
    }
    set {
      _id = value;
    }
  }

  [ManyToOne( 1, Name = "Product", ClassType = typeof( LongCrash ), Column = "`PrdKey`", Unique = true, NotNull = true
   , Update = false, Insert = false, Cascade = CascadeStyle.None
   , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto )]
  public virtual IProduct Product {
    get {
      return this._product;
    }
    set {
      _product = value;
    }
  }

  #region Other Properties
  [Property( Name = "PassengerName1", Column = "`PassengerName1`", Access = "property"
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public virtual string PassengerName1 {
    get {
      return this._PassengerName1;
    }
    set {
      if ( ( this._PassengerName1 != value ) ) {
        this.OnChanging();
        this._PassengerName1 = value;
        this.OnChanged();
      }
    }
  }
  #endregion
  #endregion

  #region Methods
  public override string ToString() {
    return Id != null ? Id.ToString() : "<null>";
  }
  #endregion
}

[Class( NameType = typeof( LongPaperWitnesses ), Table = "`LongPaperWitnesses`", Polymorphism = PolymorphismType.Explicit
, Mutable = false, Lazy = false, DynamicUpdate = false, DynamicInsert = false, OptimisticLock = OptimisticLockMode.None )]
public partial class LongPaperWitnesses : ObjectState {
  #region Fields
  private string _id;
  private IProduct _product =new ProductHeader();
  private string _WitnessName1;
  #endregion

  #region Ctors
  public LongPaperWitnesses() {
  }
  #endregion

  #region Properties

  [Id( 0, Name = "Id", Length = 100, Access = "property", TypeType = typeof( string ) )]
  [Column( 1, Name = "`PrdKey`", Length = 100, Unique = true, NotNull = true )]
  [Generator( 2, Class = "native" )]
  public virtual string Id {
    get {
      return _id;
    }
    set {
      _id = value;
    }
  }

  [ManyToOne( 1, Name = "Product", ClassType = typeof( LongCrash ), Column = "`PrdKey`", Unique = true, NotNull = true
   , Update = false, Insert = false, Cascade = CascadeStyle.None
   , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto )]
  public virtual IProduct Product {
    get {
      return this._product;
    }
    set {
      _product = value;
    }
  }

  #region Other Properties
  [Property( Name = "WitnessName1", Column = "`WitnessName1`", Access = "property"
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public virtual string WitnessName1 {
    get {
      return this._WitnessName1;
    }
    set {
      if ( ( this._WitnessName1 != value ) ) {
        this.OnChanging();
        this._WitnessName1 = value;
        this.OnChanged();
      }
    }
  }
  #endregion
  #endregion

  #region Methods
  public override string ToString() {
    return Id != null ? Id.ToString() : "<null>";
  }
  #endregion
}

[Class( NameType = typeof( ShortContributingCauses ), ProxyType = typeof( IContributingCauses ), Table = "`Short_ContributingCauses`"
, Polymorphism = PolymorphismType.Explicit, Mutable = false, Lazy = false
, DynamicUpdate = false, DynamicInsert = false, OptimisticLock = OptimisticLockMode.None )]
public partial class ShortContributingCauses : ContributingCauses, IContributingCauses {
  #region Ctors
  public ShortContributingCauses() {
  }
  #endregion

  #region Properties

  [Id( 0, Name = "Id", Length = 100, Access = "property", TypeType = typeof( string ) )]
  [Column( 1, Name = "`PrdKey`", Length = 100, Unique = true, NotNull = true )]
  [Generator( 2, Class = "native" )]
  public override string Id {
    get {
      return base.Id;
    }
    set {
      base.Id = value;
    }
  }

  [ManyToOne( 1, Name = "Product", ClassType = typeof( ShortCrash ), Column = "`PrdKey`", Unique = true, NotNull = true
   , Update = false, Insert = false, Cascade = CascadeStyle.None
   , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto )]
  public override IProduct Product {
    get {
      return base.Product;
    }
    set {
      base.Product = value;
    }
  }

  #region Other Properties
  [Property( Name = "CauseEnvironment1", /*Column*/Formula = "`CauseEnvironment1`", Access = "property"
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public override string CauseEnvironment1 {
    get {
      return base.CauseEnvironment1;
    }
    set {
      if ( ( base.CauseEnvironment1 != value ) ) {
        base.CauseEnvironment1 = value;
      }
    }
  }
  #endregion
  #endregion

  #region Methods
  public override string ToString() {
    return base.Id != null ? base.Id.ToString() : "";
  }
  #endregion
}

[JoinedSubclass( NameType = typeof( ShortTimeLocation ), ExtendsType = typeof( LOCATION ), ProxyType = typeof( ILocation )
  , Table = "`Short_TimeLocation`", Lazy = false, DynamicUpdate = false, DynamicInsert = false )]
public partial class ShortTimeLocation : LOCATION, ILocation {
  #region Fields
  private string _AgencyReportNumber;
  private IProduct _product =new ProductHeader();
  #endregion

  #region Ctors

  public ShortTimeLocation() {
  }
  #endregion

  #region Properties

  [Key( 0, Column = "`LocationColKey`" )]
  [Generator( 1, Class = "native" )]
  public override string Id {
    get {
      return base.Id;
    }
    set {
      if ( ( base.Id != value ) ) {
        base.Id = value;
      }
    }
  }

  [ManyToOne( 1, Name = "Product", ClassType = typeof( ShortCrash ), Column = "`PrdKey`", Unique = true, NotNull = true
   , Update = false, Insert = false, Cascade = CascadeStyle.None
   , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto )]
  public virtual IProduct Product {
    get {
      return this._product;
    }
    set {
      _product = value;
    }
  }

  #region Other Properties
  [Property( Name = "AgencyReportNumber", /*Column*/Formula = "`AgencyReportNumber`", Access = "property"
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public virtual string AgencyReportNumber {
    get {
      return this._AgencyReportNumber;
    }
    set {
      if ( ( this._AgencyReportNumber != value ) ) {
        this._AgencyReportNumber = value;
      }
    }
  }
  #endregion
  #endregion
}

[Class( NameType = typeof( ShortVehiclePedestrian ), ProxyType = typeof( IVehiclePedestrian ), Table = "`Short_VehiclePedestrian`"
, Polymorphism = PolymorphismType.Explicit, Mutable = false, Lazy = false, DynamicUpdate = false, DynamicInsert = false )]
public partial class ShortVehiclePedestrian : VehiclePedestrian, IVehiclePedestrian {
  #region Ctors
  public ShortVehiclePedestrian() {
  }
  #endregion

  #region Properties
  [Id( 0, Name = "Id", Length = 100, Access = "property", TypeType = typeof( string ) )]
  [Column( 1, Name = "`PrdKey`", Length = 100, Unique = true, NotNull = true )]
  [Generator( 2, Class = "native" )]
  public override string Id {
    get {
      return base.Id;
    }
    set {
      base.Id = value;
    }
  }

  [ManyToOne( 1, Name = "Product", ClassType = typeof( ShortCrash ), Column = "`PrdKey`", Unique = true, NotNull = true
   , Update = false, Insert = false, Cascade = CascadeStyle.None
   , Fetch = FetchMode.Join, OuterJoin = OuterJoinStrategy.Auto )]
  public override IProduct Product {
    get {
      return base.Product;
    }
    set {
      base.Product = value;
    }
  }

  #region Other Properties
  [Property( Name = "EstDamage1", Formula = "`EstDamage1`", Access = "property"
   , TypeType = typeof( string ), Insert = false, Update = false )]
  public override string EstDamage1 {
    get {
      return base.EstDamage1;
    }
    set {
      if ( ( base.EstDamage1 != value ) ) {
        base.EstDamage1 = value;
      }
    }
  }
  #endregion
  #endregion

  #region Methods
  public override string ToString() {
    return base.Id != null ? base.Id.ToString() : "";
  }
  #endregion
}


Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!--Generated from NHibernate.Mapping.Attributes on 2006-09-21 10:26:30Z.-->
<hibernate-mapping default-cascade="none" default-access="property" assembly="Tracs.Florida.Data.DataModels.TracsDb.Generic, Version=1.0.2455.16933, Culture=neutral, PublicKeyToken=null" xmlns="urn:nhibernate-mapping-2.0">
  <class name="Tracs.Florida.Data.DataModels.TracsDb.Generic.ProductHeader, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.IProduct, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" table="`Prd_Header`" mutable="false" polymorphism="explicit">
    <id name="Id" access="property" type="String" length="100">
      <column name="`PrdKey`" length="100" not-null="true" unique="true" />
      <generator class="native" />
    </id>
    <discriminator column="`PrdName`" />
    <property name="CaseNumber" access="property" type="String" column="`CaseNumber`" not-null="false" update="false" insert="false" />
    <property name="ProductName" access="property" type="String" column="`PrdName`" length="100" update="false" insert="false" />
  </class>
  <class name="Tracs.Florida.Data.DataModels.TracsDb.Generic.LOCATION, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.ILocation, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" table="`LOCATION`" discriminator-value="LOCATION" mutable="false" polymorphism="explicit">
    <id name="Id" access="property" type="String" length="100">
      <column name="`COLLKEY`" length="100" unique="true" />
      <generator class="native" />
    </id>
    <property name="Latitude" access="property" type="String" column="`Latitude`" not-null="false" update="false" insert="false" />
    <property name="Longitude" access="property" type="String" column="`Longitude`" not-null="false" update="false" insert="false" />
    <many-to-one name="Product" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.ProductHeader, Tracs.Florida.Data.DataModels.TracsDb.Generic" column="`PrdKey`" unique="true" cascade="none" outer-join="auto" fetch="join" update="false" insert="false" />
  </class>
  <class name="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperContributingCauses, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.IContributingCauses, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" table="`LongPaperContributingCauses`" mutable="false" polymorphism="explicit" optimistic-lock="none">
    <id name="Id" access="property" type="String" length="100">
      <column name="`PrdKey`" length="100" not-null="true" unique="true" />
      <generator class="native" />
    </id>
    <property name="CauseEnvironment1" access="property" type="String" update="false" insert="false" formula="`CauseEnvironment1`" />
    <many-to-one name="Product" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" column="`PrdKey`" not-null="true" unique="true" cascade="none" outer-join="auto" fetch="join" update="false" insert="false" />
  </class>
  <class name="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperPassenger, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" table="`LongPaperPassengers`" mutable="false" polymorphism="explicit" optimistic-lock="none">
    <id name="Id" access="property" type="String" length="100">
      <column name="`PrdKey`" length="100" unique="true" />
      <generator class="native" />
    </id>
    <property name="PassengerName1" access="property" type="String" column="`PassengerName1`" update="false" insert="false" />
    <many-to-one name="Product" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" column="`PrdKey`" not-null="true" unique="true" cascade="none" outer-join="auto" fetch="join" update="false" insert="false" />
  </class>
  <class name="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperVehiclePedestrian, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.IVehiclePedestrian, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" table="`LongPaperVehiclePedestrian`" mutable="false" polymorphism="explicit" optimistic-lock="none">
    <id name="Id" access="property" type="String" length="100">
      <column name="`PrdKey`" length="100" not-null="true" unique="true" />
      <generator class="native" />
    </id>
    <property name="EstDamage1" access="property" type="String" update="false" insert="false" formula="`EstDamage1`" />
    <many-to-one name="Product" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" column="`PrdKey`" not-null="true" unique="true" cascade="none" outer-join="auto" fetch="join" update="false" insert="false" />
  </class>
  <class name="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperWitnesses, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" table="`LongPaperWitnesses`" mutable="false" polymorphism="explicit" optimistic-lock="none">
    <id name="Id" access="property" type="String" length="100">
      <column name="`PrdKey`" length="100" not-null="true" unique="true" />
      <generator class="native" />
    </id>
    <property name="WitnessName1" access="property" type="String" column="`WitnessName1`" update="false" insert="false" />
    <many-to-one name="Product" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" column="`PrdKey`" not-null="true" unique="true" cascade="none" outer-join="auto" fetch="join" update="false" insert="false" />
  </class>
  <class name="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortContributingCauses, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.IContributingCauses, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" table="`Short_ContributingCauses`" mutable="false" polymorphism="explicit" optimistic-lock="none">
    <id name="Id" access="property" type="String" length="100">
      <column name="`PrdKey`" length="100" not-null="true" unique="true" />
      <generator class="native" />
    </id>
    <property name="CauseEnvironment1" access="property" type="String" update="false" insert="false" formula="`CauseEnvironment1`" />
    <many-to-one name="Product" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" column="`PrdKey`" not-null="true" unique="true" cascade="none" outer-join="auto" fetch="join" update="false" insert="false" />
  </class>
  <class name="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortVehiclePedestrian, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.IVehiclePedestrian, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" table="`Short_VehiclePedestrian`" mutable="false" polymorphism="explicit">
    <id name="Id" access="property" type="String" length="100">
      <column name="`PrdKey`" length="100" not-null="true" unique="true" />
      <generator class="native" />
    </id>
    <property name="EstDamage1" access="property" type="String" update="false" insert="false" formula="`EstDamage1`" />
    <many-to-one name="Product" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" column="`PrdKey`" not-null="true" unique="true" cascade="none" outer-join="auto" fetch="join" update="false" insert="false" />
  </class>
  <subclass name="Tracs.Florida.Data.DataModels.TracsDb.Generic.Crash, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.ICrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" extends="Tracs.Florida.Data.DataModels.TracsDb.Generic.ProductHeader, Tracs.Florida.Data.DataModels.TracsDb.Generic" discriminator-value="base" />
  <subclass name="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.ICrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" extends="Tracs.Florida.Data.DataModels.TracsDb.Generic.Crash, Tracs.Florida.Data.DataModels.TracsDb.Generic" discriminator-value="CRASH-LONG">
    <one-to-one name="Location" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperTimeLocation, Tracs.Florida.Data.DataModels.TracsDb.Generic" cascade="none" outer-join="auto" fetch="join" property-ref="Product" />
    <one-to-one name="ContributingCauses" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperContributingCauses, Tracs.Florida.Data.DataModels.TracsDb.Generic" cascade="none" outer-join="auto" fetch="join" property-ref="Product" />
    <one-to-one name="VehiclePedestrians" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperVehiclePedestrian, Tracs.Florida.Data.DataModels.TracsDb.Generic" cascade="none" outer-join="auto" fetch="join" property-ref="Product" />
    <one-to-one name="Passengers" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperPassenger, Tracs.Florida.Data.DataModels.TracsDb.Generic" cascade="none" outer-join="auto" fetch="join" property-ref="Product" />
    <one-to-one name="Witnesses" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperWitnesses, Tracs.Florida.Data.DataModels.TracsDb.Generic" cascade="none" outer-join="auto" fetch="join" property-ref="Product" />
  </subclass>
  <subclass name="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.ICrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" extends="Tracs.Florida.Data.DataModels.TracsDb.Generic.Crash, Tracs.Florida.Data.DataModels.TracsDb.Generic" discriminator-value="ShortCrash">
    <one-to-one name="Location" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortTimeLocation, Tracs.Florida.Data.DataModels.TracsDb.Generic" cascade="none" outer-join="auto" fetch="join" property-ref="Product" />
    <one-to-one name="ContributingCauses" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortContributingCauses, Tracs.Florida.Data.DataModels.TracsDb.Generic" cascade="none" outer-join="auto" fetch="join" property-ref="Product" />
    <one-to-one name="VehiclePedestrians" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortVehiclePedestrian, Tracs.Florida.Data.DataModels.TracsDb.Generic" cascade="none" outer-join="auto" fetch="join" property-ref="Product" />
  </subclass>
  <joined-subclass name="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperTimeLocation, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.ILocation, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" extends="Tracs.Florida.Data.DataModels.TracsDb.Generic.LOCATION, Tracs.Florida.Data.DataModels.TracsDb.Generic" table="`LongPaperTimeLocation`">
    <key column="`LocationColKey`" />
    <property name="AgencyReportNumber" access="property" type="String" not-null="false" update="false" insert="false" formula="`AgencyReportNumbert`" />
    <many-to-one name="Product" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.LongCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" column="`PrdKey`" cascade="none" outer-join="true" fetch="join" update="false" insert="false" />
  </joined-subclass>
  <joined-subclass name="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortTimeLocation, Tracs.Florida.Data.DataModels.TracsDb.Generic" proxy="Tracs.Florida.Data.DataModels.TracsDb.Generic.ILocation, Tracs.Florida.Data.DataModels.TracsDb.Generic" lazy="false" dynamic-update="false" dynamic-insert="false" extends="Tracs.Florida.Data.DataModels.TracsDb.Generic.LOCATION, Tracs.Florida.Data.DataModels.TracsDb.Generic" table="`Short_TimeLocation`">
    <key column="`LocationColKey`" />
    <property name="AgencyReportNumber" access="property" type="String" update="false" insert="false" formula="`AgencyReportNumber`" />
    <many-to-one name="Product" access="property" class="Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortCrash, Tracs.Florida.Data.DataModels.TracsDb.Generic" column="`PrdKey`" cascade="none" outer-join="auto" fetch="join" update="false" insert="false" />
  </joined-subclass>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
try {
        factory = cfg.BuildSessionFactory();
       
        session = factory.OpenSession();
        session.FlushMode = FlushMode.Never;

        #region New up a data object and set some properties
        ICrash crash = new Crash();
        crash.Location.Latitude = "30.2";
        crash.Location.Longitude = "-81.7";
        crash.CaseNumber = "35907";
        #endregion

        #region Set the QBE with an ICriteria object
        NHibernate.ICriteria criteria;
        NHibernate.Expression.Example example = NHibernate.Expression.Example.Create( crash );

        example.ExcludeNulls()
               .IgnoreCase()
               .EnableLike( NHibernate.Expression.MatchMode.Anywhere );

        criteria = session.CreateCriteria( crash.GetType() )
                          .Add( example );

        if ( crash.ContributingCauses != null && crash.ContributingCauses.Dirty ) {
          NHibernate.Expression.Example subExample = NHibernate.Expression.Example.Create( crash.ContributingCauses );

          subExample.ExcludeNulls()
                    .IgnoreCase()
                    .EnableLike( NHibernate.Expression.MatchMode.Anywhere );

          NHibernate.ICriteria subCrit = criteria.CreateCriteria( "ContributingCauses" )
                                                         .Add( subExample );
        }

        if ( crash.Location != null && crash.Location.Dirty ) {
          NHibernate.Expression.Example subExample = NHibernate.Expression.Example.Create( crash.Location );

          subExample.ExcludeNulls()
                    .IgnoreCase()
                    .EnableLike( NHibernate.Expression.MatchMode.Anywhere );

          NHibernate.ICriteria subCrit = criteria.CreateCriteria( "Location" )
                                                         .Add( subExample );
        }

        if ( crash.VehiclePedestrians != null && crash.VehiclePedestrians.Dirty ) {
          NHibernate.Expression.Example subExample = NHibernate.Expression.Example.Create( crash.VehiclePedestrians );

          subExample.ExcludeNulls()
                    .IgnoreCase()
                    .EnableLike( NHibernate.Expression.MatchMode.Anywhere );

          NHibernate.ICriteria subCrit = criteria.CreateCriteria( "VehiclePedestrians" )
                                                         .Add( subExample );
        }
        #endregion

        IList<LongCrash> results = null;

        #region Begin transaction and attempt to get an IList from the ICriteria
        try {
          if ( !session.IsConnected ) {
            session.Reconnect();
          }

          session.BeginTransaction();
          criteria.SetCacheable( true );
          results = criteria.List<LongCrash>();
          session.Transaction.Commit();
        }
        catch {
          if ( session.Transaction != null ) {
            session.Transaction.Rollback();
          }
          results = null;
          throw;
        }
        finally {
        }
        #endregion
      }
      finally {
        if ( factory != null )
          factory.Close();

        if ( session != null ) {
          if ( session.IsConnected )
            session.Disconnect();

          if ( session.IsOpen )
            session.Close();

          session.Dispose();
        }
      }


Full stack trace of any exception that occurs:

Test method TracsFloridaTestProject.TracsFloridaDataDataModelsTracsDbGenericTest.QBETest threw exception: System.InvalidCastException: Unable to cast object of type 'Tracs.Florida.Data.DataModels.TracsDb.Generic.LOCATION' to type 'Tracs.Florida.Data.DataModels.TracsDb.Generic.ShortTimeLocation'..

at (Object , GetterCallback )
at NHibernate.Bytecode.Lightweight.GetPropertyValuesInvoker.Invoke(Object obj, GetterCallback callback)
at NHibernate.Bytecode.Lightweight.AccessOptimizer.GetPropertyValues(Object target) in D:\Downloads\dotNet\NHibernate\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Bytecode\Lightweight\AccessOptimizer.cs:line 28
at NHibernate.Persister.Entity.AbstractEntityPersister.GetPropertyValues(Object obj) in D:\Downloads\dotNet\NHibernate\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:line 229
at NHibernate.Expression.Example.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) in D:\Downloads\dotNet\NHibernate\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Expression\Example.cs:line 264
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition() in D:\Downloads\dotNet\NHibernate\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Criteria\CriteriaQueryTranslator.cs:line 362
at NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, CriteriaImpl criteria, Type rootEntityName, IDictionary enabledFilters) in D:\Downloads\dotNet\NHibernate\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Criteria\CriteriaJoinWalker.cs:line 71
at NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, Type rootEntityName, IDictionary enabledFilters) in D:\Downloads\dotNet\NHibernate\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Loader\Criteria\CriteriaLoader.cs:line 45
at NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria, IList results) in D:\Downloads\dotNet\NHibernate\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 4881
at NHibernate.Impl.SessionImpl.Find[T](CriteriaImpl criteria) in D:\Downloads\dotNet\NHibernate\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\SessionImpl.cs:line 4856
at NHibernate.Impl.CriteriaImpl.List[T]() in D:\Downloads\dotNet\NHibernate\NHibernate-1.2.0.Alpha1-debug\src\NHibernate\Impl\CriteriaImpl.cs:line 285
at TracsFloridaTestProject.TracsFloridaDataDataModelsTracsDbGenericTest.QBETest()

Name and version of the database you are using:

SQL Server 2005 (2000 format)

The generated SQL (show_sql=true):

10:26:47.859 [AdpaterExeMgrThread1] DEBUG NHibernate.Loader.Entity.AbstractEntityLoader - Static select for entity Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperWitnesses: SELECT longpaperw0_.[PrdKey] as PrdKey1_9_, longpaperw0_.[PrdKey] as PrdKey1_5_9_, longpaperw0_.[WitnessName1] as WitnessN2_5_9_, longcrash1_.[PrdKey] as PrdKey1_0_, longcrash1_.[CaseNumber] as CaseNumber3_0_0_, longcrash1_.[PrdName] as PrdName2_0_0_, longpaperp2_.[PrdKey] as PrdKey1_1_, longpaperp2_.[PrdKey] as PrdKey1_3_1_, longpaperp2_.[PassengerName1] as Passenge2_3_1_, longpapert3_.[LocationColKey] as COLLKEY1_2_, longpapert3_.[PrdKey] as PrdKey2_8_2_, longpapert3_1_.[PrdKey] as PrdKey4_1_2_, longpapert3_1_.[Longitude] as Longitude3_1_2_, longpapert3_1_.[Latitude] as Latitude2_1_2_, longpapert3_.[AgencyReportNumbert] as formula4_2_, producthea4_.[PrdKey] as PrdKey1_3_, producthea4_.[CaseNumber] as CaseNumber3_0_3_, producthea4_.[PrdName] as PrdName2_0_3_, producthea4_.[PrdName] as PrdName2_3_, longpaperc5_.[PrdKey] as PrdKey1_4_, longpaperc5_.[PrdKey] as PrdKey1_2_4_, longpaperc5_.[CauseEnvironment1] as formula0_4_, longpaperv6_.[PrdKey] as PrdKey1_5_, longpaperv6_.[PrdKey] as PrdKey1_4_5_, longpaperv6_.[EstDamage1] as formula1_5_, shorttimel7_.[LocationColKey] as COLLKEY1_6_, shorttimel7_.[PrdKey] as PrdKey2_9_6_, shorttimel7_1_.[PrdKey] as PrdKey4_1_6_, shorttimel7_1_.[Longitude] as Longitude3_1_6_, shorttimel7_1_.[Latitude] as Latitude2_1_6_, shorttimel7_.[AgencyReportNumber] as formula5_6_, shortcontr8_.[PrdKey] as PrdKey1_7_, shortcontr8_.[PrdKey] as PrdKey1_6_7_, shortcontr8_.[CauseEnvironment1] as formula2_7_, shortvehic9_.[PrdKey] as PrdKey1_8_, shortvehic9_.[PrdKey] as PrdKey1_7_8_, shortvehic9_.[EstDamage1] as formula3_8_ FROM [LongPaperWitnesses] longpaperw0_ inner join [Prd_Header] longcrash1_ on longpaperw0_.[PrdKey]=longcrash1_.[PrdKey] left outer join [LongPaperPassengers] longpaperp2_ on longcrash1_.[PrdKey]=longpaperp2_.[PrdKey] left outer join [LongPaperTimeLocation] longpapert3_ on longcrash1_.[PrdKey]=longpapert3_.[PrdKey] left outer join [LOCATION] longpapert3_1_ on longpapert3_.[LocationColKey]=longpapert3_1_.[COLLKEY] left outer join [Prd_Header] producthea4_ on longpapert3_1_.[PrdKey]=producthea4_.[PrdKey] left outer join [LongPaperContributingCauses] longpaperc5_ on producthea4_.[PrdKey]=longpaperc5_.[PrdKey] left outer join [LongPaperVehiclePedestrian] longpaperv6_ on producthea4_.[PrdKey]=longpaperv6_.[PrdKey] left outer join [Short_TimeLocation] shorttimel7_ on producthea4_.[PrdKey]=shorttimel7_.[PrdKey] left outer join [LOCATION] shorttimel7_1_ on shorttimel7_.[LocationColKey]=shorttimel7_1_.[COLLKEY] left outer join [Short_ContributingCauses] shortcontr8_ on producthea4_.[PrdKey]=shortcontr8_.[PrdKey] left outer join [Short_VehiclePedestrian] shortvehic9_ on producthea4_.[PrdKey]=shortvehic9_.[PrdKey] WHERE longpaperw0_.[PrdKey]=:[PrdKey]

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: h

_________________
Timex


Last edited by inmytime3021 on Tue Sep 26, 2006 2:55 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: 1.2.0.Beta1 still yields the same problem.
PostPosted: Tue Sep 26, 2006 10:30 am 
Newbie

Joined: Wed Sep 20, 2006 5:03 pm
Posts: 10
Unfortunately, I still have the problem after trying out 1.2.0.Beta1. I really need some help with this. What baffles me is that - using the very same data model for both cases, the HQL code path works fine, but the QBE does not. What is wrong with my structure?

(I will feel like an idiot if it is something simple, but glad nonetheless.)

Thanks guys.

_________________
Timex


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 1:08 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
That's a lot to sift through... can you bold the line of code that throws the error?

My guess is that you are assuming somewhere that your list is of a certain type.... but is really a proxy or Iesi list or something.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 9:25 pm 
Newbie

Joined: Wed Sep 20, 2006 5:03 pm
Posts: 10
Thanks for your reply, subdigital.

I apologize for the lengthy case. I'll simplify it down here to what is relevant, and one can reference the full source above if needbe.

I have some tables as follows:
  • Prd_Header
    • PrdName
    • PrdKey
    • CaseNumber
  • LOCATION
    • COLLKEY
    • Latitude
    • Longitude
  • LongPaperTimeLocation
    • PrdKey
    • LocationColKey
    • AgencyReportNumbert
  • Short_TimeLocation
    • PrdKey
    • LocationColKey
    • AgencyReportNumber

Note: The fields in bold are primary/foreign keys.

The are related as such:

Graphically:
Code:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .  ____________ . . . . . ___________ . . . . . . . . . . . . . . . . .
. . . . . . | . . . . . .| . . . . |. . . . . .| . . . . . . . . . . . . . . . .
. . . . . . | Prd_Header.|. . . . .| LOCATION .| . . . . . . . . . . . . . . . .
. . . . . . |____________|. . . . .|___________|  . . . . . . . . . . . . . . . .
. . . . . . . 1 | . . .| 1. . . . . . . 1|. .|1 . . . . . . . . . . . . . . . . .
. . . . . . . . | . . .| . . . . . . .. .|. .| . . . . . . . . . . . . . . . . .
. . . . . . . . | . .__|_________________|. .| . . . . . . . . . . . . . . . . .
. . . . . . . . | . |  | . . . . . . . . . . | . . . . . . . . . . . . . . . . .
. . . . . . . 1 | . |  |__________1 . . . . .|1 . . . . . . . . . . . . . . . . .
_______________|___|___ . . _____|__________|____ . . . . . . . . . . . . . . .
| . . . . . . . . . . . | . | . . . . . . . . . . | . . . . . . . . . . . . . . .
| LongPaperTimeLocation | . | Short_TimeLocation .| . . . . . . . . . . . . . . .
|_______________________| . |_____________________| . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


Logically:

Prd_Header 1 <---Prd_Header_PrdKey_TO_LongPaperTimeLocation_PrdKey---------- 1 LongPaperTimeLocation
Prd_Header 1 <---Prd_Header_PrdKey_TO_ShortTimeLocation_PrdKey-------------- 1 ShortTimeLocation
LOCATION 1 <---LOCATION_COLLKEY_TO_LongPaperTimeLocation_LocationColKey--- 1 LongPaperTimeLocation
LOCATION 1 <---LOCATION_COLLKEY_TO_ShortTimeLocation_LocationColKey------- 1 ShortTimeLocation

So to model them, I decided to use a combination of table-per-subclass strategy and proxies to provide some kind of higher abstraction from the relational data model:
Code:
IProduct { /*...*/ }

//<class name="ProductHeader" proxy="IProduct" table="`Prd_Header`" polymorphism="implicit">
ProductHeader : IProduct {/*...*/}

//<subclass name="Crash" proxy="ICrash" extends="ProductHeader" discriminator-value="base" />
Crash : ProductHeader, IProduct, ICrash {   
  //CONSIDER: How to guarantee property construction if property is an interface?
  private ILocation _location = new LOCATION();

  public virtual ILocation Location { get { return _location; } set { _location = value; } } 
}

//<class name="LOCATION" proxy="ILocation" table="`LOCATION`" discriminator-value="LOCATION" polymorphism="implicit">
public partial class LOCATION : ILocation { /*...*/ }

//<joined-subclass name="ShortTimeLocation" proxy="ILocation" extends="LOCATION" table="`Short_TimeLocation`">
public partial class ShortTimeLocation : LOCATION, ILocation { /*...*/ }

//<joined-subclass name="LongPaperTimeLocation" proxy="ILocation" extends="LOCATION" table="`LongPaperTimeLocation`">
public partial class LongPaperTimeLocation : LOCATION, ILocation { /*...*/ }

LongCrash : Crash, ICrash {
  //<one-to-one name="Location" class="LongPaperTimeLocation" outer-join="false" fetch="select" property-ref="Product" />
  public override ILocation Location { get { return Location; } set { Location = value; } }
}

ShortCrash : Crash, ICrash {
  //<one-to-one name="Location" class="ShortTimeLocation" outer-join="false" fetch="select" property-ref="Product" />
  public override ILocation Location { get { return Location; } set { Location = value; } }
}

And below would be the mapping:

<hibernate-mapping default-cascade="none" default-access="property" assembly="Tracs.Florida.Data.DataModels.TracsDb.Generic, Version=1.0.2460.20423, Culture=neutral, PublicKeyToken=null" xmlns="urn:nhibernate-mapping-2.0">
<class name="ProductHeader" proxy="IProduct" table="`Prd_Header`" polymorphism="implicit">
<id name="Id" access="property" type="String" length="100">
<column name="`PrdKey`" length="100" not-null="true" unique="true" />
<generator class="native" />
</id>
<discriminator column="`PrdName`" />
<property name="CaseNumber" access="property" type="String" column="`CaseNumber`" />
<property name="ProductName" access="property" type="String" column="`PrdName`" length="100" />
</class>
<subclass name="Crash" proxy="ICrash" extends="ProductHeader" discriminator-value="base" />
<subclass name="LongCrash" proxy="ICrash" extends="Crash" discriminator-value="CRASH-LONG">
<one-to-one name="Location" class="LongPaperTimeLocation" outer-join="false" fetch="select" property-ref="Product" />
</subclass>
<subclass name="ShortCrash" proxy="ICrash" extends="Crash" discriminator-value="ShortCrash">
<one-to-one name="Location" class="ShortTimeLocation" outer-join="false" fetch="select" property-ref="Product" />
</subclass>
<joined-subclass name="LongPaperTimeLocation" proxy="ILocation" extends="LOCATION" table="`LongPaperTimeLocation`">
<key column="`LocationColKey`" />
<many-to-one name="Product" access="property" class="LongCrash" column="`PrdKey`" outer-join="true" fetch="select" />
<property name="AgencyReportNumber" access="property" type="String" formula="`AgencyReportNumbert`" />
</joined-subclass>
<joined-subclass name="ShortTimeLocation" proxy="ILocation" extends="LOCATION" table="`Short_TimeLocation`">
<key column="`LocationColKey`" />
<many-to-one name="Product" access="property" class="ShortCrash" column="`PrdKey`" outer-join="false" fetch="select" />
<property name="AgencyReportNumber" access="property" type="String" formula="`AgencyReportNumber`" />
</joined-subclass>
<class name="LOCATION" proxy="ILocation" table="`LOCATION`" discriminator-value="LOCATION" polymorphism="implicit">
<id name="Id" access="property" type="String" length="100">
<column name="`COLLKEY`" length="100" unique="true" />
<generator class="native" />
</id>
<property name="Latitude" access="property" type="String" column="`Latitude`" />
<property name="Longitude" access="property" type="String" column="`Longitude`" />
</class>
</hibernate-mapping>

Once again, the exception I get is:

Test method TracsFloridaTestProject.TracsFloridaDataDataModelsTracsDbGenericTest.QBETest threw exception: System.InvalidCastException: Unable to cast object of type 'Tracs.Florida.Data.DataModels.TracsDb.Generic.LOCATION' to type 'Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperTimeLocation'.


I tried to highlight the code of significance. So it seems to be something to do with the ILocation, LOCATION, LongPaperTimeLocation objects, and their relationships. I don't understand why NHibernate cannot deduce from a LOCATION to a LongPaperTimeLocation, since have an "is a" relationship. This is a very common structure. Consider a case when working with IEnumerable types: typeof(IEnumerable).IsAssignableFrom(myObject). I do this very thing in my code when I walk my object graph. I MUST be missing something?

The problem definitely seems to do with the proxies and subclasses. I have had problems with NHibernate handling interface hierarchies and interface-to-classes especially. I guess I still don't have a good handle on the correct way to do this kind of modeling. Can anyone point out what I may be doing wrong?

And, why does the data model actually WORK for the HQL API, but not the QBE API?

Thanks

_________________
Timex


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 9:51 pm 
Newbie

Joined: Wed Sep 20, 2006 5:03 pm
Posts: 10
Quote:
My guess is that you are assuming somewhere that your list is of a certain type.... but is really a proxy or Iesi list or something.


I think you may be on to something there. I'm looking into that but intially still don't see it, quite. However, one part bothers me:
Code:
Crash : ProductHeader, IProduct, ICrash {   
  //CONSIDER: How to guarantee property construction if property is an interface?
  private ILocation _location = new LOCATION();

  public virtual ILocation Location { get { return _location; } set { _location = value; } } 
}


There, I have no choice but to new up the base LOCATION object since I cannot new up an interface. I also considered leaving it uninitialized and forcing clients to new it up, but that is a bad solution as the object model shoudl gaurantee that any object properties are too initialized when any parent object is constructed. Besides, it also led to other problems with the QBE API:

Test method TracsFloridaTestProject.TracsFloridaDataDataModelsTracsDbGenericTest.QBETest threw exception: NHibernate.ADOException: could not execute query[SQL: SELECT this_.[PrdKey] as PrdKey1_0_2_, this_.[PrdDate] as PrdDate3_0_2_, this_.[PrdTime] as PrdTime4_0_2_, this_.[OfficerName] as OfficerN5_0_2_, this_.[Agency] as Agency6_0_2_, this_.[BadgeNumber] as BadgeNum7_0_2_, this_.[CaseNumber] as CaseNumber8_0_2_, this_.[Status] as Status9_0_2_, this_.[ContactKey] as ContactKey10_0_2_, this_.[ContactDate] as Contact11_0_2_, this_.[ContactTime] as Contact12_0_2_, this_.[Modified] as Modified13_0_2_, this_.[PrdName] as PrdName2_0_2_, this_.[ContactDesc] as Contact14_0_2_, this_.[Description] as Descrip15_0_2_, this_.[PrdName] as PrdName2_2_, longpapert1_.[LocationColKey] as COLLKEY1_1_0_, longpapert1_.[PrdKey] as PrdKey2_16_0_, longpapert1_1_.[PrdKey] as PrdKey2_1_0_, longpapert1_.[AgencyReportNumber] as formula451_0_, longpapert1_.[CrashDate] as formula452_0_, longpapert1_.[CrashTime] as formula453_0_, longpapert1_.[HSMVNumber] as formula454_0_, longpapert1_.[OfficerArrived] as formula455_0_, longpapert1_.[OfficerNotified] as formula456_0_, longpapert1_.[TimeOfCrashHour] as formula457_0_, longpapert1_.[TimeOfCrashMinute] as formula458_0_, longpapert1_.[TimeOfficerArrivedHour] as formula459_0_, longpapert1_.[TimeOfficerArrivedMinute] as formula460_0_, longpapert1_.[TimeOfficerNotifiedHour] as formula461_0_, longpapert1_.[TimeOfficerNotifiedMinute] as formula462_0_, longpapert1_.[TypeOfFormCheckBox] as formula463_0_, longpapert1_1_.[LocToolVersion] as formula0_0_, longpapert1_1_.[AtNodeNo] as formula1_0_, longpapert1_1_.[CheckBoxDirection] as formula2_0_, longpapert1_1_.[CityTown] as formula3_0_, longpapert1_1_.[CountyCode] as formula4_0_, longpapert1_1_.[CountyDistanceFeet] as formula5_0_, longpapert1_1_.[CountyDistanceMile] as formula6_0_, longpapert1_1_.[CountyName] as formula7_0_, longpapert1_1_.[DivUndiv] as formula8_0_, longpapert1_1_.[FromNode] as formula9_0_, longpapert1_1_.[IntersectingRoad] as formula10_0_, longpapert1_1_.[IntersectionDirection] as formula11_0_, longpapert1_1_.[IntersectionDistanceFeet] as formula12_0_, longpapert1_1_.[IntersectionDistanceMile] as formula13_0_, longpapert1_1_.[IsTown] as formula14_0_, longpapert1_1_.[Latitude] as formula15_0_, longpapert1_1_.[LiteralDescription] as formula16_0_, longpapert1_1_.[LocationFt] as formula17_0_, longpapert1_1_.[LocationMi] as formula18_0_, longpapert1_1_.[LocatorToolUsed] as formula19_0_, longpapert1_1_.[Longitude] as formula20_0_, longpapert1_1_.[NearestIntersectingRoad] as formula21_0_, longpapert1_1_.[NextNode] as formula22_0_, longpapert1_1_.[NodeDistanceFeet] as formula23_0_, longpapert1_1_.[NodeDistanceMile] as formula24_0_, longpapert1_1_.[NoLanes] as formula25_0_, longpapert1_1_.[OfNode] as formula26_0_, longpapert1_1_.[RoadName] as formula27_0_, longpapert1_.[LocationColKey] as COLLKEY1_1_1_, longpapert1_.[PrdKey] as PrdKey2_17_1_, longpapert1_1_.[PrdKey] as PrdKey2_1_1_, longpapert1_.[CrashDate] as formula464_1_, longpapert1_.[CrashTime] as formula465_1_, longpapert1_.[OfficerNotified] as formula466_1_, longpapert1_.[OfficerArrived] as formula467_1_, longpapert1_.[AgencyReportNumbert] as formula468_1_, longpapert1_.[HSMVNumber] as formula469_1_, longpapert1_1_.[LocToolVersion] as formula0_1_, longpapert1_1_.[AtNodeNo] as formula1_1_, longpapert1_1_.[CheckBoxDirection] as formula2_1_, longpapert1_1_.[CityTown] as formula3_1_, longpapert1_1_.[CountyCode] as formula4_1_, longpapert1_1_.[CountyDistanceFeet] as formula5_1_, longpapert1_1_.[CountyDistanceMile] as formula6_1_, longpapert1_1_.[CountyName] as formula7_1_, longpapert1_1_.[DivUndiv] as formula8_1_, longpapert1_1_.[FromNode] as formula9_1_, longpapert1_1_.[IntersectingRoad] as formula10_1_, longpapert1_1_.[IntersectionDirection] as formula11_1_, longpapert1_1_.[IntersectionDistanceFeet] as formula12_1_, longpapert1_1_.[IntersectionDistanceMile] as formula13_1_, longpapert1_1_.[IsTown] as formula14_1_, longpapert1_1_.[Latitude] as formula15_1_, longpapert1_1_.[LiteralDescription] as formula16_1_, longpapert1_1_.[LocationFt] as formula17_1_, longpapert1_1_.[LocationMi] as formula18_1_, longpapert1_1_.[LocatorToolUsed] as formula19_1_, longpapert1_1_.[Longitude] as formula20_1_, longpapert1_1_.[NearestIntersectingRoad] as formula21_1_, longpapert1_1_.[NextNode] as formula22_1_, longpapert1_1_.[NodeDistanceFeet] as formula23_1_, longpapert1_1_.[NodeDistanceMile] as formula24_1_, longpapert1_1_.[NoLanes] as formula25_1_, longpapert1_1_.[OfNode] as formula26_1_, longpapert1_1_.[RoadName] as formula27_1_ FROM [Prd_Header] this_ inner join [Short_TimeLocation] longpapert1_ on this_.[PrdKey]=longpapert1_.[PrdKey] left outer join [LOCATION] longpapert1_1_ on longpapert1_.[LocationColKey]=longpapert1_1_.[COLLKEY] inner join [LongPaperTimeLocation] longpapert1_ on this_.[PrdKey]=longpapert1_.[PrdKey] left outer join [LOCATION] longpapert1_1_ on longpapert1_.[LocationColKey]=longpapert1_1_.[COLLKEY] WHERE this_.[PrdName] in ('base', 'ShortCrash', 'CRASH-LONG') AND (lower(this_.[CaseNumber]) like ?) and (lower(longpapert1_1_.[Latitude]) like ? and lower(longpapert1_1_.[Longitude]) like ?)] ---> System.Data.SqlClient.SqlException: The correlation name 'longpapert1_' is specified multiple times in a FROM clause..

This occurs if I new up a subclass to set as the property state that has the interface:
Code:
  ICrash crash = new Crash();
  //DEVNOTE: ICrash.Location property is of type ILocation.
  crash.Location = new LongPaperTimeLocation();       
  crash.Location.Latitude = "30.2";
  crash.Location.Longitude = "-81.7";
  crash.CaseNumber = "35907";
  //...set the criteria
  session.BeginTransaction();
  criteria.SetCacheable( true );
  IList<ICrash> results = criteria.List<ICrash>();
  session.Transaction.Commit();


...Still battling it out. I'm hoping a hereo will come along and whisp me up and out of danger!

_________________
Timex


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 9:49 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I am doing this:

Code:
private IList _someCollection = new ArrayList();


and it works fine. Since _someCollection is an IList, it can be swapped out with Iesi.Collections.ISet very easily (or whatever the Iesi list is).

I don't think you'll have any problems if you just new up a standard collection that implements IList.[/code]

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 11:25 am 
Newbie

Joined: Wed Sep 20, 2006 5:03 pm
Posts: 10
Yes that is true, if my entities were collections. But they are not. They are single entity instances as per the 1:1 relationships in the database (yes it is a legacy database and has little normalization):

Code:
Crash : ProductHeader, IProduct, ICrash {   
  //Not a IList. New-ed up with a base concrete class. Should be possible to downcast to the appropriate subclass by NHibernate during object hydration.
  private ILocation _location = new LOCATION();

  public virtual ILocation Location { get { return _location; } set { _location = value; } } 
}


So subdigital, are you suggesting that I use collections as a workaround to solve this problem? Ironically, I once had it this way and worked fine of course. But it was not a good OO view of the data model as I had to reference by indexers (even though there will only be one in the collection) everywhere like this:

Code:
crash.Locations[0].Latitude = "-81.7";
crash.Locations[0].Longitude = "30.2";


I wanted to clean it up to interact like this:

Code:
crash.Location.Latitude = "-81.7";
crash.Location.Longitude = "30.2";


So, is there no way to do a field of an interface type that is a single instance as in my scenario (see above)?

Thanks

_________________
Timex


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 1:08 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Quote:
So subdigital, are you suggesting that I use collections as a workaround to solve this problem?


NO! You're right that is ugly. I have to admit that I am skimming here b/c I'm under time constraints. Maybe I'll have more time to look at it later.

For now I'd suggest NOT using the query.List<T> and use the non-generic one and see if that helps. I don't want to give you any more shot-in-the-dark suggestions until I've fully read your problem.

/apologies

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 2:05 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
inmytime3021 wrote:
//<subclass name="Crash" proxy="ICrash" extends="ProductHeader" discriminator-value="base" />
Crash : ProductHeader, IProduct, ICrash {
//CONSIDER: How to guarantee property construction if property is an interface?
private ILocation _location = new LOCATION();

public virtual ILocation Location { get { return _location; } set { _location = value; } }
}

LongCrash : Crash, ICrash {
//<one-to-one name="Location" class="LongPaperTimeLocation" outer-join="false" fetch="select" property-ref="Product" />
public override ILocation Location { get { return Location; } set { Location = value; } }
}

[/code]
<subclass name="LongCrash" proxy="ICrash" extends="Crash" discriminator-value="CRASH-LONG">
<one-to-one name="Location" class="LongPaperTimeLocation" outer-join="false" fetch="select" property-ref="Product" />
</subclass>

And, why does the data model actually WORK for the HQL API, but not the QBE API?


I'm having a wild guess here...

In LongCrash class You are creating LOcation to be of Location class. By mapping, the location *must* be instance of LongPaperTimeLocation. So, when QBE kicks in and encounters the Location proeprty, it rightfully assumes (According to mapping!) that it can cast Location to LongPaperTimeLocation subclass. But You have "cheated" and supplied instance with Location of type "Location"...

What happens if You create correct type of Location for subclasses (LongPaperTimeLocation for LongCrash)?

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 3:56 pm 
Newbie

Joined: Wed Sep 20, 2006 5:03 pm
Posts: 10
Quote:
What happens if You create correct type of Location for subclasses (LongPaperTimeLocation for LongCrash)?


Do you mean make each subclass maintain it's own state? Like this:

Code:
public partial class Crash : ProductHeader, ICrash, IProduct {   
  private ILocation _location = new LOCATION();
  //...
}

public partial class ShortCrash : Crash, ICrash, IProduct {
  private ILocation _location = new ShortTimeLocation(); 
  //...
}

public partial class LongCrash : Crash, ICrash, IProduct {
  private ILocation _location = new LongPaperTimeLocation();
  //...
}


And here is the mapping:

<subclass name="ShortCrash" proxy="ICrash" extends="Crash" discriminator-value="ShortCrash">
<one-to-one name="Location" access="property" class="ShortTimeLocation" outer-join="false" fetch="select" property-ref="Product" />
</subclass>

<subclass name="LongCrash" proxy="ICrash" extends="Crash" discriminator-value="CRASH-LONG">
<one-to-one name="Location" access="property" class="LongPaperTimeLocation" outer-join="false" fetch="select" property-ref="Product" />
</subclass>

I tried that but still get the casting exception:

Test method TracsFloridaTestProject.TracsFloridaDataDataModelsTracsDbGenericTest.QBETest threw exception: System.InvalidCastException: Unable to cast object of type 'Tracs.Florida.Data.DataModels.TracsDb.Generic.LOCATION' to type 'Tracs.Florida.Data.DataModels.TracsDb.Generic.LongPaperTimeLocation'..

Gert, Did you mean something different?

I'm still baffeled: Why does it work with the HQL API but not the QBE API!? I am thinking this is a bug.

_________________
Timex


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 2:50 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
inmytime3021 wrote:
Code:
public partial class Crash : ProductHeader, ICrash, IProduct {   
  private ILocation _location = new LOCATION();
  //...
}

public partial class LongCrash : Crash, ICrash, IProduct {
  private ILocation _location = new LongPaperTimeLocation();
  //...
}


Gert, Did you mean something different?


Depends how the rest of the class is declared. But having two fields with same name into class hierarchy sounds like a bad idea.

Actually, I did mean something like
Code:
public partial class LongCrash : Crash, ICrash, IProduct {
  LongCrash(){
    Location = new LongPaperTimeLocation();
  }


Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 12:33 pm 
Newbie

Joined: Wed Sep 20, 2006 5:03 pm
Posts: 10
Sorry for the miscommunication, Gert.

Well I tried that as you suggested and still no go. I get exactly the same error message. I really think it is a bug in the NHibernate.ICriteria implementation as it works great if dones as HQL. But I need the flexibility of QBE to allow users to do ad hoc querying in an analysis-driven manner. I'd hate to have to build my own query parser! QBE is honestly one of the biggest features that attracted me to NHibernate (and well, gee, OO model, OO querying/interaction, no SQL, lazy loading, caching, and the list goes on).

Another interesting point is that if I do a QBE, passing an example object that has no criteria set (just a new-ed up object), it works just fine, too. It is when it has to handle those nested one-to-one types that it bonks out, no matter how I map it and how I model it in objects! I don't want to have to revert back to IList collections, just so it will work! :(

Do you guys think it is an NHibernate bug, or still thinking it is a problem on my end? I've exhausted every advice and tried everything.

_________________
Timex


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 3:21 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
inmytime3021 wrote:
Do you guys think it is an NHibernate bug, or still thinking it is a problem on my end? I've exhausted every advice and tried everything.


Could You put together a compileable test program that shows the bug? I would try to debug a little...

Gert

_________________
If a reply helps You, rate it!


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