samee ullah
samee ullah

Reputation: 13

Nhibernate - Getting two object type in repository return result

I have two class BookingInfo.cs and BookingTransaction class.

  public class BookingInfo
    {
      public virtual string Code { get; set; }
    }

  public class BookingTransaction : BookingInfo   {
        public virtual string CustomerRefNo { get; set; }
     }

below is the NHibernate mapping for both classes

   public class BookingInfoConfiguration : ClassMap<BookingInfo> {
            public BookingInfoConfiguration() {
                Table("Bkg_BookingInfo");
                LazyLoad();
                DynamicUpdate();
                Id(x => x.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty);
        }
    }

      public class BookingTransactionConfiguration : 
     ClassMap<BookingTransaction> {
            public BookingTransactionConfiguration() {
                Table("Bkg_BookingInfo");
                LazyLoad();
                DynamicUpdate();
                Id(x => x.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty);
    }
}

Now i am querying to get rows from database.

 CurrentSession.Query<BookingInfo>().ToList();

I get two items for single row in database table. one for Bookinginfo and another for BookingTransaction. but i want to get only result of type Bookinginfo.

How to remove the items of the child class from the result?

Upvotes: 1

Views: 351

Answers (2)

Fr&#233;d&#233;ric
Fr&#233;d&#233;ric

Reputation: 9864

As said in Rabban's answer, this is by design. This is called implicit polymorphism. Rabban propose you to change your class hierarchies, but you can instead disable implicit polymorphism if you prefer.

With hbm mappings (I do not use fluent and do not know it), add the attribute polymorphism="explicit" on your class.

Mapping by code supports it too on class mapper with .Polymorphism(PolymorphismType.Explicit).

You can read more about implicit/explicit polymorphism here:

Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or the class and that instances of any subclass of the class will be returned by a query that names the class itself. Explicit polymorphism means that class instances will be returned only be queries that explicitly name that class and that queries that name the class will return only instances of subclasses mapped inside this <class> declaration as a <subclass> or <joined-subclass>. For most purposes the default, polymorphism="implicit", is appropriate. Explicit polymorphism is useful when two different classes are mapped to the same table (this allows a "lightweight" class that contains a subset of the table columns).

Upvotes: 1

Rabban
Rabban

Reputation: 2581

Its intended that NHibernate returns you both objects. To prevent this behavior create a abstract base class where both other class derive. Then you don't need to duplicate code and you can query each class separately.

Create the base class:

public abstract class BookingBase
{
    public virtual string Code { get; set; }
}

and then derive your classes from it:

public class BookingInfo : BookingBase
{
}
public class BookingTransaction : BookingBase
{
    public virtual string CustomerRefNo { get; set; }
}

Your mappings and queries can remain the same. And if you want to query both classes with one query, just query BookingBase.

Upvotes: 1

Related Questions