NHibernate标准:如何排除某些映射的属性/集合? [英] NHibernate Criteria: howto exclude certain mapped properties/collections?

查看:202
本文介绍了NHibernate标准:如何排除某些映射的属性/集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的(简化的)模型:票证->客户回叫(一个或多个)

Here's my (simplified) model: Ticket -> Customer Callback (s)

我已对我的票证进行了映射,以便在加载票证时也可以进行回调.

I have my Ticket mapped so that when it's loaded, the Callbacks are as well.

        base.HasMany<TechSupportCallback>(x => x.Callbacks)  
            .KeyColumn(Fields.TRACKED_ITEM_ID)
            .Not.LazyLoad()
            .Inverse()
            .Cache.ReadWrite();

这不是延迟加载,因为否则,当Web服务尝试序列化(并加载)代理时,我将得到没有会话以加载实体". (使用存储库来获取数据.)

This is not lazy loading because otherwise I'll get 'no session to load entities' when the web service tries to serialize (and load) the proxy. (Using repositories to fetch data.)

它也是双向的..(在CallbackMap中)

It's also bi-directional .. (in the CallbackMap)

        base.References(x => x.Ticket)
            .Column(Fields.TRACKED_ITEM_ID)
            .Not.Nullable();


现在..我们需要向座席显示他们的回调列表-仅是他们的回调. -当我使用条件"查询回调时,无法阻止加载工单(以及随后的整个图,包括其他集合).我以前曾尝试设置FetchMode.Lazy,然后迭代每个结果Callback并将Ticket设置为null,但这似乎被忽略了.


Now .. we need to show an agent a list of their callbacks - JUST their callbacks. -- When I query using Criteria for the Callbacks, I cannot prevent the Ticket (and subsequently it's entire graph, including other collections) from being loaded. I had previously tried to set FetchMode.Lazy, then iterate each resulting Callback and set Ticket to null, but that seems to be ignored.

             // open session & transaction in using (..)
                var query = session.CreateCriteria<TechSupportCallback>()
                    .SetCacheable(true)
                    .SetCacheRegion("CallbacksByUserAndGroups")
                    .SetFetchMode("Ticket", FetchMode.Lazy) // <-- but this doesn't work!
                    .SetMaxResults(AegisDataContext.Current.GetMaxRecordCount())
                    ;
                rValue = query.List<TechSupportCallback>();
                rvalue.ForEach(x => x.Ticket = null;); // <-- since this is already retrieved, doing this merely prevents it from going back across the wire
                tx.Commit();
             // usings end (..)

我应该用投影来代替吗? 问题..是我无法找到用于填充实体或它们的列表的投影的示例-只能用作子实体的子查询或类似的限制条件父实体列表.

Should I be doing this with a projection instead? The problem with that .. is I've not been able to find an example of projections being used to populate an entity, or a list of them -- only to be used as a subquery on a child entity or something similar to restrict a list of parent entities.

我真的可以在这方面使用一些指导.

I could really use some guidance on this.

我尝试按照建议使用投影,但是:

I tried using a projection as suggested but:

我得到以下信息:(这是由于一个错误,因此我已经停止使用缓存,并且可以使用.

I'm getting the following: (this was because of a bug, and so I've since stopped using the cache and it works. http://nhibernate.jira.com/browse/NH-1090)

System.InvalidCastException occurred
  Message=Unable to cast object of type 'AEGISweb.Data.Entities.TechSupportCallback' to type 'System.Object[]'.
  Source=NHibernate
  StackTrace:
       at NHibernate.Cache.StandardQueryCache.Put(QueryKey key, ICacheAssembler[] returnTypes, IList result, Boolean isNaturalKeyLookup, ISessionImplementor session)
  InnerException: 

at

 rValue = query.List<TechSupportCallback>();

定义的投影列表类似

 // only return the properties we want!
 .SetProjection(Projections.ProjectionList()
     .Add(Projections.Alias(Projections.Id(), ex.NameOf(x => x.ID))) // 
     .Add(Projections.Alias(Projections.Property(ex.NameOf(x => x.ContactID)), ex.NameOf(x => x.ContactID)))
     // ...
  )
  .SetResultTra...;
  rValue = query.List<TechSupportCallback>();

映射为

    public TechSupportCallbackMap()
    {
        base.Cache.ReadWrite();
        base.Not.LazyLoad();

        base.Table("TS_CALLBACKS");

        base.Id(x => x.ID, Fields.ID)
            .GeneratedBy.Sequence("SEQ_TS_CALLBACKS");

        base.References(x => x.Ticket)
            .Column(Fields.TRACKED_ITEM_ID)
            .Not.Nullable();

        base.Map(x => x.TrackedItemID, Fields.TRACKED_ITEM_ID)
            .Not.Insert()
            .Not.Update()
            .Generated.Always()
            ;
        // ...
     }

推荐答案

这听起来像是针对投影的一项工作.

This sounds like it's a job exactly for projections.

var query = session.CreateCriteria<TechSupportCallback>()
                    .SetCacheable(true)
                    .SetCacheRegion("CallbacksByUserAndGroups")
                    .SetFetchMode("Ticket", FetchMode.Lazy)
                    .SetMaxResults(AegisDataContext.Current.GetMaxRecordCount())
                    .SetProjection(Projections.ProjectionList().
                                         .Add(Projections.Alias(Projections.Id(), "Id")
                                         .Add(Projections.Alias(Projections.Property("Prop"), "Prop")))
                    .SetResultTransformer(Transformers.AliasToBean<TechSupportCallback>())
                    ;

仅列出您要包括的所有属性,并排除故障单实体.您甚至可以创建POCO类,仅用于封装此查询的结果.而不是使用现有的实体类.

Simply list all the properties you want to include and exclude the Ticket entity. You can even create a POCO class simply for encapsulating the results of this query. Rather than using an existing entity class.

这篇关于NHibernate标准:如何排除某些映射的属性/集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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