在nhibernate中加入queryover

时间:2011-04-27 15:19:49

标签: c# nhibernate queryover

我正在尝试在NHibernate中使用QueryOver创建查询,但没有成功。 我需要得到的结果如下:

select mn.Story_ID from Membership_Updates mn
join Updates upd on mn.Story_ID = upd.Story_ID
where mn.MembershipUser_ID = 1
group by mn.Story_ID having MAX(mn.DismissTime) <= MAX(upd.CreationDate)

我的课程:

public class MembershipUpdates
{
  public MembershipUpdates()
  {
    DismissTime = DateTime.Now;
  }

  public virtual int Id  { get; set; }
  public virtual MembershipUser User { get; set; }
  public virtual Story Story { get; set; }
  public virtual DateTime DismissTime { get; set; }
}

public class Updates 
{
  public Updates()
  {
    CreationDate = DateTime.Now;
  }

  public virtual int Id  { get; set; }
  public virtual DateTime CreationDate { get; private set; }
  public virtual Story Story { get; set; }
  public virtual string Message{ get; set; }
}

也许我错过了一些明显的东西

1 个答案:

答案 0 :(得分:2)

我可能错了,但我认为QueryOver不允许你加入不相关的实体(不使用子查询)。

我会使用HQL:

select mn.Story
from MembershipUpdates mn, Updates upd
where upd.Story = upd.Story
and mn.User.id = 1
group by mn.Story
having MAX(mn.DismissTime) <= MAX(upd.CreationDate)

旁注:您的实体类名称应该是单数。