Using Joins in NHibernate 2.1.1.2 through Criteria API
I am newbie to NHibernate. We have two tables AccountDailyInfo and AccountEvent in the database. AccountDailyInfo has a composite key. AccountEvent has a foreign key relationship to the composite key of AccountDailyInfo. So accordingly in our entity classes we have an
-IList of AccountEvent in AccountDailyInfo called as AccountEventList
-A property of type AccountDailyInfo in AccountEvent
Now we want to perform a join on these two tables. I have been able to produce a simple join using the below statement.
_ses.CreateCriteria<AccountDailyInfo>().CreateAlias("Account开发者_运维问答EventList", "foreignKey", NHibernate.SqlCommand.JoinType.InnerJoin)
.List<AccountDailyInfo>()
But here we are not able to apply a filter criteria based on the value in AccountEvent table. Similarly in one overall query we are at least using join on 4 to 5 tables. And in the end we are filtering out the result based on values from all different tables.
I request someone to provide me one simple and one complex Criteria query about how multiple joins will work and how can we apply filtering to the same. Also is it possible to join tables which have no mapping relationship between them ?
Simply use the alias you provided to filter on the join. So in your case it's 'foreignkey' So you could do something like
criteria.Add(Restrictions.Eq("foreignkey.Name", "SomeEventName"));
精彩评论