I have an Nhibernate model which inherits properties from a base model but uses a different table (view):
public class ChildModel : ParentModel
{
public virtual string ChildProperty1 {get; set;}
...
}
public class ParentModel
{
public virtual int Id { get; set; }
...
}
The mapping of the child looks like this:
public class ChildClassMap : ClassMap<ChildModel>
{
public ChildClassMap()
{
Table(ParentViewEnhanced);
//MapAllThePropertiesFromBaseviaStaticHelper
Map(x => x.ChildProperty1).Column("Property1");
}
}
In the db I have a table Parent which is mapped to ParentModel and a view ParentViewEnhanced which selects all properties from Parent and some additional properties from other tables. It looks like this:
create view ParentViewEnhanced
AS select p.*,
s.xyz,
....
FROM Parent LEFT JOIN subtable s ON (...)
With this I want to create a generic QueryOver which should query one of the above models depending on some value, so I have:
T m_Alias = null; //T is generic parameter of ParentModel
IQueryOver<T, T> query = mySession.QueryOver(() => m_Alias);
Where T should be either ChildModel or ParentModel.
The problem is that no matter what kind of result I try to get from the query, the same queryover is exectuted for both tables / views:
NHibernate: Select ... from Parent where ...
NHibernate: Select ... from ParentViewEnhanced where ...
This only happens when I query the parent table (Parent). When T is set to type ChildModel, only the query responsible for ParentViewEnhanced is executed (as expected)
e.g. I try to run:
query.Select(Projections.ProjectionList().Add(Projections.CountDistinct<T>(x => x.Id)))
.UnderlyingCriteria.UniqueResult<int>()
and I get the exception:
query did not return a unique result: 2
because as I said it queried both parent and subtable(view) when I expected it to only search in the parent / child...
Note that as in the child view there is a left join the model uses a composite key (the join gets limited by setting some properties in the queryover...). Other than that I have no idea how to resolve this issue or where it is coming from. When I use Session.Get(..) instead of QueryOver on either model only one table / view is accessed.