4

The scenario is a MVC.NET application using NHibernate to interact with a SQL Server database.

Every NHibernate query with Criteria works fine, except for the last I have added, that give the following error:

could not resolve property: Reading.Truck.Company.CompanyId of: ADO.Alarms

Related code is the following:

session = NHibernateSessionManager.Instance.GetSession();
            ICriteria criteria = session.CreateCriteria(typeof(Alarms));
            criteria.Add(Expression.Eq("Reading.Truck.Company.CompanyId", companyId));

            alarmsList = criteria.List<Alarms>();

Nested properties are defined in related class file like this: Alarms.cs

private AlarmsReading _Reading;  
public AlarmsReading Reading
    {
        get { return _Reading; }
        set { _Reading= value; }
    }

These properties are defined in mapping files like this:

Alarms.hbm.xml

<hibernate-mapping  ...>
<class name="Alarms" table="Alarms" lazy="false">
<cache usage="read-write"/>
...
<many-to-one name="Reading" class="AlarmsReading">
  <column name="Reading_Id" not-null="true"/>
</many-to-one>
...
</class>
</hibernate-mapping>

All other criteria query, with other nested properties, defined same way, works fine.

Strange thing is if I use normal IQuery syntax with createQuery method, all works smoothly; that is:

session = NHibernateSessionManager.Instance.GetSession();
IQuery query = null;
query = session.CreateQuery("FROM Alarms al WHERE Reading.Truck.Company.CompanyId = (:companyId) ");
query.SetParameter("companyId", companyId);
alarmsList = query.List<Alarms>();

I've also find this similar question, and I know there are various way to solve, but I'm looking for the cause of this misbehaviour.

Any help will be appreciated. Thank you in advance.

2
  • 1
    HQL (which is what CreateQuery accepts) is parsed differently than queries built using Criteria or QueryOver. In this case, the HQL query is probably expanded to 3 or 4 joins (try profiling it to see). Why doesn't Criteria support this kind of querying? The short answer is just that it wasn't designed that way. Commented Nov 13, 2015 at 16:36
  • @Andrew Thanks, it is so. I've done a further analysis and found solution here. Commented Nov 16, 2015 at 13:23

1 Answer 1

1

Ok. I've understood how to use Criteria properly, reading answers to this post, and following this example on the NHibernate official site.

Dot notation with Criteria seems to work only for the components of the current object (and their properties: i.e. Reading.ReadingId).

If we want to reach deeper properties we would use Associations.

So this code works fine for my task:

session = NHibernateSessionManager.Instance.GetSession();
ICriteria criteria = session.CreateCriteria(typeof(Alarms));
criteria.CreateCriteria("Reading").CreateCriteria("Truck")
        .Add(Expression.Eq("Company.CompanyId", companyId));

alarmsList = criteria.List<Alarms>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.