对引用实体进行限制的 NHibernate 查询
我正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要事先使用别名连接到“语言”表。完整的查询应该是:
更多信息位于: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:
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.