NHibernate参考资料任何拉回错误类型的东西 [英] NHibernate ReferencesAny pulling back the wrong type

查看:81
本文介绍了NHibernate参考资料任何拉回错误类型的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为AdministratorPrivilages的表,该表具有以下字段:

I've got a table called AdministratorPrivilages that has the following fields:

  • ID
  • 列表项
  • 会员编号
  • 会员类型

现在,成员可以是两种类型(Enterprise和Express).企业成员位于企业表中. Express成员生活在expressmember表中.我已经尝试过像这样做流利的贴图.

Now, the members can be of two types (Enterprise and Express). Enterprise members live in the enterprise table. Express members live in the expressmember table. I've tried to do my fluent mapping like so.

public class AdministratorPrivilegesMapping : ClassMap<AdministratorPrivileges>
    {
        public AdministratorPrivilegesMapping()
        {
            Id(x=>x.Id);
            Map(x => x.Value).Column("Value");
            ReferencesAny(x => x.Member)
                .EntityTypeColumn("MemberType")
                .EntityIdentifierColumn("MemberId")
                .IdentityType<Int32>()
                .AddMetaValue<ExpressMember>("Express")
                .AddMetaValue<Member>("Enterprise");

        }
    }

两个成员表都有带递增值的整数ID.当我尝试拉回与企业成员10关联的特权时,我得到与Express成员10关联的权限集.其他两个表都与旧式的hbm映射文件映射.

Both member tables have integer ids with ascending values. When I try to pull back the privilages associated with enterprise member 10, I'm getting the permission set associated with Express Member 10. Both other tables are mapped with the old school hbm mapping files.

我缺少明显的东西吗?我正在使用NHibernate 2.1和FluentNhibernate 1.1

Am I missing something obvious? I'm using NHibernate 2.1 and FluentNhibernate 1.1

推荐答案

我实际上是使用.Where()找到解决方案的. 我的情况有些不同,我有objectA和objectB. objectA包含objectB,但是objectB也包含objectBs的集合.

I actually found the solution using .Where(). My situation is a little different, I have objectA and objectB. objectA contains objectB, but objectB also contains a collection of objectBs.

"objectA":{ "someProp":(string)", "objectB":{ "someProp":(string)", "comeCol":[ (objectB)" ] } }

"objectA":{ "someProp" : "(string)", "objectB":{ "someProp" : "(string)", "comeCol" : [ "(objectB)" ] } }

所以objectB的父对象"属性可以是objectB或objectA类型,这就是为什么我需要在objectB的映射中使用ReferencesAny.

So the "Parent" property of objectB can either be of type objectB or objectA, which is why I needed to use ReferencesAny in the mapping of objectB.

映射看起来像

        ReferencesAny( x => x.Parent )
            .IdentityType< int >()
            .MetaType< string >()
            .EntityTypeColumn( "ParentType" )
            .EntityIdentifierColumn( "ParentId" )
            .AddMetaValue< objectA >( "E" )
            .AddMetaValue< objectB >( "S" )
            .Access.Property()
            .LazyLoad()
            .Cascade.All();

所有这些在保存时都很好用,但是,在检索时发生了我的问题,因为没有告知框架要检索什么,只是简单地检索了所有内容.

All this works well when saving, however, my problem occured when retrieving, because the framework wasn't told what to retrieve and simply retrieved everything.

所以现在这是修复问题的集合的映射:

So now here is the mapping of the collection that fixed the problem:

        HasMany( x => x.objectBs )
            .KeyColumn( "ParentId" )
            .Where( "ParentType = 'S'" )
            .Cascade.All()
            .LazyLoad();

希望它可以帮助处于相同情况的任何人:)

Hope it will help anyone in the same situation :)

这篇关于NHibernate参考资料任何拉回错误类型的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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