NHibernate针对字典(地图)的关键字段进行查询 [英] NHibernate query against the key field of a dictionary (map)

查看:114
本文介绍了NHibernate针对字典(地图)的关键字段进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象模型,其中Calendar对象具有一个名为UserPermissions的IDictionary<MembershipUser, Perms>,其中MembershipUser是一个对象,而Perms是一个简单的枚举.这在日历的映射文件中为

I have an object model where a Calendar object has an IDictionary<MembershipUser, Perms> called UserPermissions, where MembershipUser is an object, and Perms is a simple enumeration. This is in the mapping file for Calendar as

<map name="UserPermissions" table="CalendarUserPermissions" lazy="true" cascade="all">
  <key column="CalendarID"/>
  <index-many-to-many class="MembershipUser" column="UserGUID" />
  <element column="Permissions" type="CalendarPermission" not-null="true" />
</map>

现在,我想执行查询以查找给定用户具有某些权限的所有日历.许可无关紧要;我只想要一个日历列表,其中给定用户在UserPermissions词典中作为键出现.我具有用户名属性,而不是MembershipUser对象.如何使用QBC(或HQL)构建它?这是我尝试过的:

Now I want to execute a query to find all calendars for which a given user has some permission defined. The permission is irrelevant; I just want a list of the calendars where a given user is present as a key in the UserPermissions dictionary. I have the username property, not a MembershipUser object. How do I build that using QBC (or HQL)? Here's what I've tried:

ISession session = SessionManager.CurrentSession;
ICriteria calCrit = session.CreateCriteria<Calendar>();
ICriteria userCrit = calCrit.CreateCriteria("UserPermissions.indices");
userCrit.Add(Expression.Eq("Username", username));
return calCrit.List<Calendar>();

此构造的无效SQL-WHERE子句按预期包含WHERE membership1_.Username = @p0,但是FROM子句不包含MemberhipUsers表.

This constructed invalid SQL -- the WHERE clause contained WHERE membership1_.Username = @p0 as expected, but the FROM clause didn't include the MemberhipUsers table.

此外,我真的不得不努力学习.indices表示法.我通过浏览NHibernate源代码找到了它,并发现还有.elements和其他一些点分的符号.在哪里引用关联路径的允许语法?我觉得上面的内容非常接近,只是缺少一些简单的内容.

Also, I really had to struggle to learn about the .indices notation. I found it by digging through the NHibernate source code, and saw that there's also .elements and some other dotted notations. Where's a reference to the allowed syntax of an association path? I feel like what's above is very close, and just missing something simple.

推荐答案

我自己尝试做这件事,看起来可以使用HQL来完成,但不能使用Criteria API.

Just trying to do this myself and it looks like this can be done with HQL but not the Criteria API.

https://nhibernate.jira.com/browse/NH-1795

要在HQL中进行:

http://ayende.com/Blog/archive/2009/06/03/nhibernate-mapping-ndash-ltmapgt.aspx

专门查看Ayende的评论:

Specifically look for Ayende's comment:

类似于: 从配置文件p加入p中选择1条目e 其中index(e)='HasCats'和e ='true'

It is something like: select 1 from Profile p join p.Entries e where index(e) = 'HasCats' and e = 'true'

这篇关于NHibernate针对字典(地图)的关键字段进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆