0

I am using NHibernate with fluent mapping. I have the following scenario:

public class A
{
     public virtual int ID{get; set;}
     public virtual B BReference {get; set;}
     public virtual string aProperty {get; set;}
}

public class B
{
    public virtual int ID {get;set;}
    public virtual string Name {get; set;}
}

public class MapA: Map<A>
{
    Id(x=> x.ID);
    Map (x=> x.aProperty);
    References(x => x.BReference);
}

public class MapB : Map<B>
{
    Id(x=>x.ID);
    Map(x=>x.Name);
}

What i want to do is to filter A objects on the property A.BReference.Name. I am using the queryover to do my queries:

public object GetResult(ISession session)
{
   var result = session.QueryOver<A>.Where(a=> a.BReference.Name=="Value").List();
   return result;
} 

Currently NHibernate is throwing an exception Could not resolve property BReference.Name of: A.

Is there some way to overcome this propblem? I can't change the queryover pattern now because we have dependence on it. Thanks for your help.

razlebe
  • 7,134
  • 6
  • 42
  • 57
Manar Husrieh
  • 491
  • 1
  • 5
  • 16

1 Answers1

1

Hi you have to use JoinAlias to load the related table before you write a criteria.

B bReference = null;
var result = session.QueryOver<A>()
.JoinAlias(x => x.BReference, () => bReference, NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Where(x => bReference.Name == "Value").List<A>();
Marian Ban
  • 8,158
  • 1
  • 32
  • 45