FluentNHibernate:嵌套组件映射导致NHiberate QueryException

时间:2011-07-21 13:33:58

标签: fluent-nhibernate nhibernate-mapping

嗨我在Nhibernate中有映射问题。当我运行一个linq查询引用我的实体的一个组件类时,我得到一个QueryException,如下所示:

  

无法解析属性:ClosedCases:Project.Entities.Headline

我有一个记录表,我希望将其映射到标题实体的多个对象中。

问题:

  

我的地图中是否设置了错误的内容?

我的Headline实体分为多个逻辑类,如下所示

  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; }
  }

映射文件:

  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");
        });
      });

我正在运行的Linq查询如下所示:

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);

我知道我可以通过团队ID使用团队实体来访问标题项目并且这样可行,只是对已实现目标的映射存在问题。 任何想法?

1 个答案:

答案 0 :(得分:4)

我认为您的映射很好。但据我所知,无法使用Linq查询嵌套组件到NHibernate。它无法理解连接要做什么,它变得太复杂了。

我认为使用JoinQueryOver或JoinAlias可以使用NHibernate 3的QueryOver API。你应该读这个:QueryOver in NH 3.0

也许这样的事情会起作用:

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>();