使用 Criteria API 的 nHibernate 多对多查询
在询问之前,我已经查看了有关此主题的所有相关帖子
我有团队和成员,他们之间存在多对多关系
基本上:成员 -> 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过为
Teams
路径调用CreateAlias
,NHibernate 将为您连接适当的表。无需对成员 ID 进行子查询:但是,您甚至没有使用别名,因此您可能更愿意急切地获取团队:
这样做将确保您在访问每个
时不会再次访问数据库成员
的Teams
集合。By virtue of calling
CreateAlias
for theTeams
path, NHibernate will join the appropriate tables for you. There's no need to do the subquery on member IDs:However, you're not even using the alias, so you might just prefer to eagerly get the teams:
Doing so will ensure you do not hit the database again when accessing each
Member
'sTeams
collection.