在 NHibernate/Hibernate 中查询聚合对象
我有一个国家和州的域模型,如下所示(见下文)。
我想(使用 Crieteria API 或 HQL)获取特定国家/地区的所有州。我收到 CountryCode 作为参数。
根据我对 NHibernate 的理解,我必须加载国家/地区,然后使用“国家/地区对象”向我的第二个存储库发出调用,以便能够在我的 crieria 中创建 Expression.Eq()。有没有办法获取特定国家/地区的所有州,从而使用单个查询?我只想做一个简单的 SQL 内部联接,然后添加对国家/地区代码的约束。
我确信它与投影有关,但我发现的唯一示例是针对单个模型并展示如何使用聚合函数,这不是我打算做的。
非常感谢您的帮助!
我当前的存储库调用如下所示:
public IList<Model.StateProvinces> LoadStateProvincesForAutocomplete(string partialName, string countryCode)
{
CountryRepository countryRepo = new CountryRepository();
Model.Country currentCountry = countryRepo.Get(countryCode);
return
_session.CreateCriteria<Model.StateProvince>()
.Add(Expression.Eq("Country", currentCountry))
.Add(Expression.Like("Name", partialName, MatchMode.Anywhere))
.List<Model.StateProvince>();
}
我的模型定义如下:
public class Country
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string NameEn { get; set; }
public virtual string NameFr { get; set; }
public virtual List<Model.StateProvince> StateProvinces { get; set; }
}
public class StateProvince
{
public virtual Country Country { get; set; }
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string NameEn { get; set; }
public virtual string NameFr { get; set; }
}
I have a Domain Model for Countries and States that looks like the following(see below).
I want to (with the Crieteria API or HQL) fetch all States for a specific country. I receive as a parameter the CountryCode.
From what I understand in NHibernate, I must load the country and then issue the call to my second repository with the "Country Object" to be able to make an Expression.Eq() in my crieria. Is there any way to fetch all States for a specific Country and therefore, use a single query ? I just want to do a simple SQL inner join and and then add a constraint on the country code.
I'm sure it has something to do with projections but the only examples I've found are for a single model and show how to use aggreate function which is not what I intend to do.
Thank you very much for your help !
My current repository call looks like this :
public IList<Model.StateProvinces> LoadStateProvincesForAutocomplete(string partialName, string countryCode)
{
CountryRepository countryRepo = new CountryRepository();
Model.Country currentCountry = countryRepo.Get(countryCode);
return
_session.CreateCriteria<Model.StateProvince>()
.Add(Expression.Eq("Country", currentCountry))
.Add(Expression.Like("Name", partialName, MatchMode.Anywhere))
.List<Model.StateProvince>();
}
And my models are defined as follow :
public class Country
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string NameEn { get; set; }
public virtual string NameFr { get; set; }
public virtual List<Model.StateProvince> StateProvinces { get; set; }
}
public class StateProvince
{
public virtual Country Country { get; set; }
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string NameEn { get; set; }
public virtual string NameFr { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 ICriteria 的 lambda 扩展,应该会非常简单:
只要两个实体的关系在您的 HBM 中正确映射,就应该添加适当的联接和等式约束。
另外,尝试使用 NHibernate.Linq 命名空间来使用 Linq2NH:
当您阅读本文时,我正在设置更深层次的 Linq 查询;一点汗也没有。
Should be pretty simple if you use the lambda extensions to ICriteria:
This ought to add the proper join and equality constraint, as long as the two entities' relationship is properly mapped in your HBMs.
Also, try Linq2NH with the NHibernate.Linq namespace:
I'm setting up Linq queries as you read this that go a couple layers deeper; no sweat at all.