NHibernate映射一个类的层次结构,其基类是抽象的,鉴别器不是一个字符串 [英] NHibernate mapping with a class hierarchy whose base class is abstract and the discriminator is not a string

查看:165
本文介绍了NHibernate映射一个类的层次结构,其基类是抽象的,鉴别器不是一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public abstract class BaseClass 
{
...

$ b $ public class ChildClass:BaseClass
{
...
}

请注意,父类是抽象的,这是什么给了我一些困难的时候来映射与流利的nhibernate。我的鉴别器是一个字节(DB中的tinyint)。因为它不是一个字符串,我不能设置歧义值的基类,这是行不通的(从BaseClass的映射类):

<$ ()类型
.SubClass< ChildClass>()
.IsIdentifiedBy((byte)OperationType.Plan)
.MapSubClassColumns (p => {...})

我得到的错误信息是:



类初始化方法UnitTest1.MyClassInitialize抛出异常。 NHibernate.MappingException:NHibernate.MappingException:无法将鉴别值的格式格式化为实体的SQL字符串BaseClass ---> System.FormatException:输入的字符串格式不正确。



<下面的文章似乎解释了会发生什么。他们用xml给出了一个解决方案,但没有流利的nhibernate: http://forum.hibernate.org / b
$ b $ p



<$ p

我已经找到了一个解决方法,但是这看起来像是一个补丁...
我添加了以下内容到映射文件: $ p> SetAttribute(discriminator-value,-1);

它似乎指示FNH不要使用字符串(我认为它使用类名)抽象基类。
为了使它与-1值一起工作,我也将我的鉴别器类型从字节改为了字节。

编辑:
我错过了:这是DiscriminateSubClassesOnColumn采用默认值的第二个参数。所以我的问题的正确答案是:
$ b $ pre $ DiscriminateSubClassesOnColumn< sbyte>(Type,(sbyte)-1)


Here are the domain model classes:

public abstract class BaseClass
{
...
}

public class ChildClass : BaseClass
{
...
}

Note that the parent class is abstract and this is what gives me some difficulties when comes the time to map with fluent nhibernate. My discriminator is a byte (tinyint in the DB). Because it is not a string and I can't manage to set a discriminator value on the base class, this does not work (taken from the mapping class for BaseClass):

DiscriminateSubClassesOnColumn<byte>("Type")
    .SubClass<ChildClass>()
    .IsIdentifiedBy((byte)OperationType.Plan)
    .MapSubClassColumns(p => { ... })

The error message I get is:

Class Initialization method UnitTest1.MyClassInitialize threw exception. NHibernate.MappingException: NHibernate.MappingException: Could not format discriminator value to SQL string of entity BaseClass ---> System.FormatException: Input string was not in a correct format..

The following post seems to explain what happens. They give a solution with xml but not with fluent nhibernate: http://forum.hibernate.org/viewtopic.php?t=974225

Thanks for the help.

解决方案

I have found a workaround but this seems so like a patch... I added the following to the mapping file:

SetAttribute("discriminator-value", "-1");

It seems to instruct FNH not to use a string (I think it uses the class name) for the abstract base class. To make it work with the -1 value, I also changed my discriminator type from byte to sbyte.

Edit: I missed that: this is the second parameter to DiscriminateSubClassesOnColumn that takes the default value. So the correct answer to my question is:

DiscriminateSubClassesOnColumn<sbyte>("Type", (sbyte)-1)

这篇关于NHibernate映射一个类的层次结构,其基类是抽象的,鉴别器不是一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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