开发者

NHibernate query-over with restriction on a referenced entity

I am using Nhibernate with SQL server 2008.

I am trying to execute the following code:

        var localization = session.QueryOver<T>()
            .Where(idFilter).AndRestrictionOn(x => x.Language.IETFTag).IsLike(tag + "%").SingleOrDefault();

However, I get an exception on that line that says nhibernate cannot resolve property Language.IETFTag (or something to that effect).

I have tried using JoinQueryOver() but then it complains that I have multiple correlations in the FROM clause or something weird like that. It simply feels like I am doing something very wrong. How can I do what I want?

I have the following mapping:

internal class LocalizationMapping : ClassMap<Localization>
{
    public LocalizationMapping()
    {
        UseUnionSubclassForInheritanceMapping();
        Id(x => x.Id).GeneratedBy.HiLo("HiLo", "NextHi", "1000");
        References(x => x.Language);
    }
}

internal class LanguageMapping : ClassMap<Language>
{
    public LanguageMapping()
    {
        Id(x => x.Id);
        Map(x => x.DefaultName);
        Map(x => x.IETFTag开发者_JS百科);
    }
}

internal class ArticleLocalizationMapping : SubclassMap<ArticleLocalization>
{
    public ArticleLocalizationMapping()
    {
        Map(x => x.Name);
        Map(x => x.Description);
        References(x => x.Article);
    }
}


You need to previously do a join to the "Language" table using aliases. The complete query should be:

Language language = null;

var localization = session.QueryOver<Localization>()
            .JoinAlias(x => x.Language, () => language)
            .Where(idFilter)
            .AndRestrictionOn(() => language.IETFTag).IsLike(tag, MatchMode.End)
            .SingleOrDefault();

More info at: http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx

Also, the "IsLike" method accepts a second arg with the matching type. I took the liberty to update your instruction with it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜