How to make multiple connections to the NHibernate API

I have the following data model:

Page
- Id      // Pk
- Type    // int

Section
- Id      // Pk
- Page    // Fk

Comment
- Id      // Pk
- Section // Fk
- Date    // DateTime

I try to request all comments related to a specific page (Say page.id = 2 and page.Type = 1) during the term. I tried this as follows:

   var criteria = session.CreateCriteria<Comment>()

   .Add(Restrictions.Eq("Section.Page.Id", pageId))
   .Add(Restrictions.Eq("Section.Page.Type", pageType))
   .Add(Restrictions.Ge("Date", start))
   .Add(Restrictions.Lt("Date", end));

However, this fails because I get the error message: "Failed to resolve property: Page from: TestNamespace.Comment". This usually indicates display errors, but works in all other cases, so I tend to believe that the error lies in the request.

Even worse, Comment.Section may be empty in some cases (there are comments that are not related to a section or page). In this case, I want to ignore these comments.

Any tips?

Thank!

+5
2
  var criteria = session.CreateCriteria<Comment>()
     .CreateAlias("Section", "section")
     .CreateAlias("section.Page", "page")
     .Add(Restrictions.Eq("page.Id", pageId))
     .Add(Restrictions.Eq("page.Type", pageType))
     .Add(Restrictions.Ge("Date", start))
     .Add(Restrictions.Lt("Date", end));

, . ​​, 3- 2- ..

+4

CreateCriteria

var criteria = session.CreateCriteria<Comment>()
               .Add(Restrictions.Ge("Date", start))
               .Add(Restrictions.Lt("Date", end))
               .CreateCriteria("Section")
               .CreateCriteria("Page")
               .Add(Restrictions.Eq("Id", pageId))
               .Add(Restrictions.Eq("Type", pageType));
0

All Articles