It looks like you could use filters for this.
First you need to define filter types
public class SpecificCategoryFilter : FilterDefinition { public SpecificCategoryFilter() { WithName("SpecificCategory").WithCondition("Category = 'A constant expression'"); } } public class SpecificLanguageFilter : FilterDefinition { public SpecificLanguageFilter() { WithName("SpecificLanguage").WithCondition("Language = 'A constant expression'"); } }
EDITED: according to the comments there is no .ApplyFilter<TFilter>()
on References()
, therefore updated so that, in my opinion, the way to do this is with filters
Filters should be applied in smooth displays.
public class Table2Map : ClassMap<Table2> { public Table2Map() {
Finally, when you open a session, you need to enable filters
using (var session = sessionFactory.OpenSession()) { session.EnableFilter("SpecificCategory"); session.EnableFilter("SpecificLanguage"); }
If you use the ICurrentSessionContext
implementation, and filters should always be applied, you can enable filters in the session returned from the call to ICurrentSessionContext.CurrentSession()
.
Now when you request Table1
to activate filters for Table2
, you need to specify NHibernate to join the reference Table2
; you can do it using
Fetch(t => t.Table2).Eager
JoinQueryOver(t => t.Table2)
(and similar join strategies)
Without NHibernate instructing to create a connection, the link will load by default and therefore filters will not be applied in the request. The downside is that Table2
will be impatient, but I don't know how to use filters otherwise. Next request
session.QueryOver<Table1>().Inner.JoinQueryOver(t => t.Table2).List();
results in SQL similar
SELECT this_.Id as Id0_1_, this_.Table2Id as Table3_0_1_, table2_.Id as Id1_0_, table2_.Category as Category1_0_, table2_.Language as Language1_0_ FROM Table1 this_ inner join Table2 table2_ on this_.Table2Id=table2_.Id WHERE table2_.Category = 'A constant expression' and table2_.Language = 'A constant expression'
which is akin to the SQL you have in your question.