嵌套对象属性的Nhibernate顺序

时间:2017-06-14 13:31:31

标签: c# nhibernate

我希望根据anested对象的属性对数据进行排序。

这是主要的“公告”类:

[Serializable]
public class Bulletin
{
    public virtual Int32 BulletinID { get; set; }
    public virtual Int32 AgentID { get; set; }
    public virtual Recipient Recipient
    {
        get
        {
            var rm = new CRecipientManager();
            var rec = rm.GetById(1);
            return rec;
        }
    }
    public virtual DateTime? DateSent { get; set; }
    public virtual DateTime DateEntry { get; set; }
}

hbm.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Sfa.Engine.domain.Bulletin,Sfa.Engine" table="Bulletins" lazy="true">
    <id name="BulletinID" column="BulletinID">
      <generator class="native" />
    </id>
    <property name="AgentID" column="AgentID" type="Int32" not-null="true" />   
    <property name="DateSent" column="DateSent" type="datetime" not-null="false" />
    <property name="DateEntry" column="DateEntry" type="datetime" not-null="true" />
  </class>
</hibernate-mapping>

这是第二个“收件人”:

public class Recipient
{
    public virtual Int32 RecipientID { get; set; }
    public virtual String Prefix { get; set; }
    public virtual String Firstname { get; set; }
    public virtual String Lastname { get; set; }
    public virtual String Email { get; set; }
    public virtual DateTime DateEntry { get; set; }
}

我的经理代码:

public ICollection<Bulletin> GetAll()
{
    using (ISession session = NHibernateHelper.OpenSession())
    {
        var bulletins = session
            .CreateCriteria(typeof(Bulletin))
            .AddOrder(Order.Asc("BulletinID"))
            .CreateAlias("Recipient", "rec").AddOrder(Order.Asc("rec.Firstname"))
            .List<Bulletin>();
        return bulletins;
    }
}

我创建了一个类Recipient的别名,并使用Firstname作为排序条件,但是nhibernate给我这个: 无法解析属性:收件人:Sfa.Engine.domain.Bulletin

Bulletin对象的收件人属性是可访问和公开的,有什么问题?

1 个答案:

答案 0 :(得分:0)

检查:

    public ICollection<Bulletin> GetAll()
    {
        using (ISession session = null)
        {
            var bulletins = session
                .CreateCriteria(typeof(Bulletin))
                .CreateAlias("Recipient", "rec")
                .SetProjection(Projections.ProjectionList()
                    .Add(Projections.Property("rec.Firstname"), "Firstname")
                )
                .AddOrder(Order.Asc("BulletinID"))
                .AddOrder(Order.Asc("Firstname"))
                .List<Bulletin>();
            return bulletins;
        }
    }

请参阅this