FluentNHibernate: nested component matching results in NHiberate QueryException

Hi, I have a display problem in Nhibernate. When I run a linq query, referencing one of the component classes of my object, I get a QueryException, as shown below:

Failed to resolve property: ClosedCases of: Project.Entities.Headline

I have one table with records that I want to map to several objects of my header object.

Question:

Is something incorrectly configured in my mapping?

My Headline object is divided into several logical classes, as you can see below

  public class Headline:Entity { public virtual DateTime Date { get; set; } public virtual TeamTarget Teamtarget { get; set; } } public class TeamTarget : Entity { public virtual DateTime FromDate { get; set; } public virtual DateTime ToDate { get; set; } public virtual TargetItem AchievedTarget { get; set; } public virtual Team Team { get; set; } } public class TargetItem : Entity { public virtual decimal ClosedCases { get; set; } public virtual decimal Invoicing { get; set; } } 

Mapping file:

  public class HeadlineMap: ClassMap<Headline> { public HeadlineMap() { Table("Headlines.Headlines"); Id(x => x.Id).Column("HeadlinesId"); Map(x => x.Date); Component(x => x.Teamtarget, t => { t.References(x => x.Team).Column("TeamId").Cascade.None(); t.Component(x => x.AchievedTarget, ti => { ti.Map(x => x.Invoicing).Column("TeamInvoicing"); ti.Map(x => x.ClosedCases).Column("TeamClosedCases"); }); }); 

My Linq query looks like this:

 decimal closedCases = _headlineRepository.All .Where(x => x.Date >= DateTimeExtensionMethods.FirstDayOfMonthFromDateTime(selectMonthFromDate) && x.Date <= DateTimeExtensionMethods.LastDayOfMonthFromDateTime(selectMonthFromDate) && x.Teamtarget.Team.Id == teamId ).Average(x => x.Teamtarget.AchievedTarget.ClosedCases); 

I know that I can access the header element using the command object by the command identifier, and this works, just a problem with displaying the achieved goal. any ideas?

+4
source share
1 answer

I think your mapping is fine. But as far as I know, it is not possible to request a nested component with Linq in NHibernate. He cannot understand what needs to be done, he becomes too complicated.

I think this is possible using the NHibernate 3 QueryOver API, using JoinQueryOver or JoinAlias. You should read the following: QueryOver in NH 3.0

Perhaps something like this will work:

 Headline headlineAlias = null; TeamTarget targetAlias = null; Team teamAlias = null; TargetItem targetItemAlias = null; var query = session.QueryOver<Headline>(() => headlineAlias) .JoinAlias(() => headlineAlias.Teamtarget, () => targetAlias) .JoinAlias(() => targetAlias.Team, () => teamAlias) .JoinAlias(() => targetAlias.AchievedTarget, () => targetItemAlias) .Where(x => x.Date >= DateTimeExtensionMethods.FirstDayOfMonthFromDateTime(selectMonthFromDate) && x.Date <= DateTimeExtensionMethods.LastDayOfMonthFromDateTime(selectMonthFromDate)) .And(() => teamAlias.Id == teamId) .Select(Projections.Avg(() => targetItemAlias.ClosedCases)) .SingleOrDefault<decimal>(); 
+4
source

All Articles