对引用实体进行限制的 NHibernate 查询

发布于 2024-11-03 05:23:20 字数 1180 浏览 3 评论 0原文

我正在将 Nhibernate 与 SQL Server 2008 一起使用。

我试图执行以下代码:

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

但是,我在该行上收到一个异常,表示 nhibernate 无法解析属性 Language.IETFTag (或类似的内容)。

我尝试过使用 JoinQueryOver() 但后来它抱怨我在 FROM 子句中有多个相关性或类似的奇怪的东西。感觉就像我在做一些非常错误的事情。我怎样才能做我想做的事?

我有以下映射:

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);
    }
}

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

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);
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2024-11-10 05:23:20

您需要事先使用别名连接到“语言”表。完整的查询应该是:

Language language = null;

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

更多信息位于:http: //nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx

另外,“IsLike”方法接受具有匹配类型的第二个参数。我冒昧地用它更新了您的说明。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文