Search code examples
c#nhibernatecastle-activerecord

NHibernate.QueryException ActiveRecord


   [ActiveRecord]   
    public class Category
   {
    private int _id;
    private string _name;
    private Category _category;

    [PrimaryKey(PrimaryKeyType.HiLo, "id", Params = "max_lo=9")]
    public long Id
    {
        get { return _id; }
        protected internal set { _id = value; }
    }
    [Property]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    [BelongsTo("ParentCategoryId")]
    public Category ParentCategory
    {
        get { return _category;}
        set { _category = value; }
    }       
}

Table is correctly generated in the database and data can be insterted without any problems

But when I'm running

        var criteria = DetachedCriteria.For<Category>
            .Add(Restrictions.Eq("ParentCategory.ParentCategoryId", testCategory.id));           
        Assert.That(m_repository.FindAll(criteria).Length, Is.EqualTo(1));

I'm reciving QueryException

`NHibernate.QueryException : could not resolve property: ParentCategory.ParentCategoryId'

Any idea?


Solution

  • You are fetching the ParentCategory property of a Category, which is itself a Category. Your DetachedCriteria should be:

       var criteria = DetachedCriteria.For<Category>()
                                      .Add(Restrictions.Eq("ParentCategory.Id",
                                                           testCategory.id));