使用 Criteria API 的 nHibernate 多对多查询

发布于 2024-08-21 19:41:39 字数 1568 浏览 3 评论 0原文

在询问之前,我已经查看了有关此主题的所有相关帖子

,我还阅读了此博客文章:http://ayende.com/Blog/archive/2007/12/23/NHiberante-Querying-Many-To-Many-associations -using-the-Criteria-API.aspx

我有团队和成员,他们之间存在多对多关系

基本上:成员 -> MemberTeam <- Team

通过我的查询,我尝试获取与被查询成员属于同一团队的所有成员(包括被查询成员)

我使用以下 FluentHibernate 创建了表:

TeamMap 代码:

Id(x => x.ID).GeneratedBy.GuidComb().UnsavedValue("00000000-0000-0000-0000-000000000000");

HasManyToMany(x => x.Members)
    .Table("MemberTeam")
    .ChildKeyColumn("TeamID")
    .ParentKeyColumn("MemberID");

MemberMap 代码:

Id(x => x.ID).GeneratedBy.GuidComb().UnsavedValue("00000000-0000-0000-0000-000000000000");

HasManyToMany(x => x.Teams)
    .Table("MemberTeam")
    .ChildKeyColumn("MemberID")
    .ParentKeyColumn("TeamID");

我执行查询的代码是:

DetachedCriteria dCriteria = DetachedCriteria.For(typeof(Team), "team")
    .SetProjection(Projections.Id())
    .Add(Property.ForName("team.ID").EqProperty("mt.ID"));

ICriteria criteria = Session.CreateCriteria(typeof (Member), "member")
    .CreateAlias("Teams", "mt")
    .Add(Subqueries.Exists(dCriteria))
    .Add(Restrictions.Eq("mt.MemberID", new Guid(memberID)));

IList<Member> list = criteria.List<Member>();

我知道我做错了,但我不明白它是什么。

任何帮助将不胜感激,

非常感谢!

PS 我的地图接缝很好,我可以很好地保存对象!

Before asking I have looked at all relevant posts on this topic

I have also read this blog post: http://ayende.com/Blog/archive/2007/12/23/NHiberante-Querying-Many-To-Many-associations-using-the-Criteria-API.aspx

I have Teams and I have Members, there is many-to-many relationship between them

Basically: Member -> MemberTeam <- Team

With my query I try to get all members that belong to same team as queried member (including queried member)

I have created my tables using Following FluentHibernate:

TeamMap code:

Id(x => x.ID).GeneratedBy.GuidComb().UnsavedValue("00000000-0000-0000-0000-000000000000");

HasManyToMany(x => x.Members)
    .Table("MemberTeam")
    .ChildKeyColumn("TeamID")
    .ParentKeyColumn("MemberID");

MemberMap code:

Id(x => x.ID).GeneratedBy.GuidComb().UnsavedValue("00000000-0000-0000-0000-000000000000");

HasManyToMany(x => x.Teams)
    .Table("MemberTeam")
    .ChildKeyColumn("MemberID")
    .ParentKeyColumn("TeamID");

The code I do my query with is:

DetachedCriteria dCriteria = DetachedCriteria.For(typeof(Team), "team")
    .SetProjection(Projections.Id())
    .Add(Property.ForName("team.ID").EqProperty("mt.ID"));

ICriteria criteria = Session.CreateCriteria(typeof (Member), "member")
    .CreateAlias("Teams", "mt")
    .Add(Subqueries.Exists(dCriteria))
    .Add(Restrictions.Eq("mt.MemberID", new Guid(memberID)));

IList<Member> list = criteria.List<Member>();

I know that I'm doing something wrong, but I can not understand what it its

Any help would be appreciated

Thank You very much!

P.S. My map seams to be fine, I can save objects just fine!

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

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

发布评论

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

评论(1

没︽人懂的悲伤 2024-08-28 19:41:39

通过为 Teams 路径调用 CreateAlias,NHibernate 将为您连接适当的表。无需对成员 ID 进行子查询:

var members = session
    .CreateCriteria<Member>("member")
    .CreateAlias("Teams", "mt")
    .List<Member>();

但是,您甚至没有使用别名,因此您可能更愿意急切地获取团队:

var members = session
    .CreateCriteria<Member>("member")
    .SetFetchMode("Teams", FetchMode.Eager)
    .List<Member>();

这样做将确保您在访问每个 时不会再次访问数据库成员Teams集合。

By virtue of calling CreateAlias for the Teams path, NHibernate will join the appropriate tables for you. There's no need to do the subquery on member IDs:

var members = session
    .CreateCriteria<Member>("member")
    .CreateAlias("Teams", "mt")
    .List<Member>();

However, you're not even using the alias, so you might just prefer to eagerly get the teams:

var members = session
    .CreateCriteria<Member>("member")
    .SetFetchMode("Teams", FetchMode.Eager)
    .List<Member>();

Doing so will ensure you do not hit the database again when accessing each Member's Teams collection.

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