Discriminator type other than String

250 views
Skip to first unread message

Steven Harman

unread,
Apr 15, 2009, 1:30:51 PM4/15/09
to Fluent NHibernate
Has anyone ever tried to use something other than String as the
DiscrimitatorType when using table-per-hierarchy for inheritance? For
example, I want to do the following:

public class CommentMapping : ClassMap<Comment>
{
public CommentMapping()
{
Id(x => x.Id);
DiscriminateSubClassesOnColumn<bool>("IsOfficial")
.SubClass<UserComment>(false, m => {})
.SubClass<OfficialComment>(true, m => {});

Map(x => x.Body).WithMaxSize().Not.Nullable();
Map(x => x.CreatedAt).Not.Nullable();
}
}

with an object hierarchy that looks like:

public abstract class Comment : Entity { }

public class UserComment : Comment {}

public class OfficialComment : Comment {}


With this configuration, I get the following exception:
NHibernate.MappingException: Could not format discriminator value to
SQL string of entity VersionOne.Innovations.Model.Comment --->
System.FormatException: String was not recognized as a valid Boolean.
at System.Boolean.Parse(String value)
at NHibernate.Type.BooleanType.FromStringValue(String xml)
at NHibernate.Type.BooleanType.StringToObject(String xml)
at NHibernate.Persister.Entity.SingleTableEntityPersister..ctor
(PersistentClass persistentClass, ICacheConcurrencyStrategy cache,
ISessionFactoryImplementor factory, IMapping mapping)
--- End of inner exception stack trace ---
at NHibernate.Persister.Entity.SingleTableEntityPersister..ctor
(PersistentClass persistentClass, ICacheConcurrencyStrategy cache,
ISessionFactoryImplementor factory, IMapping mapping)
at NHibernate.Persister.PersisterFactory.CreateClassPersister
(PersistentClass model, ICacheConcurrencyStrategy cache,
ISessionFactoryImplementor factory, IMapping cfg)
at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg,
IMapping mapping, Settings settings, EventListeners listeners)
at NHibernate.Cfg.Configuration.BuildSessionFactory()

Tuna Toksoz

unread,
Apr 15, 2009, 1:33:23 PM4/15/09
to fluent-n...@googlegroups.com
What is the type in the database? bit?

Tuna Toksöz
Eternal sunshine of the open source mind.

http://devlicio.us/blogs/tuna_toksoz
http://tunatoksoz.com
http://twitter.com/tehlike

Steven Harman

unread,
Apr 15, 2009, 1:35:42 PM4/15/09
to Fluent NHibernate
Yes, the database field gets generated as:

IsOfficial BIT not null

-steve

On Apr 15, 1:33 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
> What is the type in the database? bit?
>
> Tuna Toksöz
> Eternal sunshine of the open source mind.
>
> http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitter.com/tehlike

Tuna Toksoz

unread,
Apr 15, 2009, 1:36:24 PM4/15/09
to fluent-n...@googlegroups.com
Ups wait, bool type asks for "true" "false" kind of thing. I wil lfind.

Steven Harman

unread,
Apr 15, 2009, 2:02:03 PM4/15/09
to Fluent NHibernate
Tuna, thanks for the quick replies... but I'm not sure I understand
your last post. Are you saying you think this might be a NHibernate
issue with parsing bit fields for bools?

Thanks,
-steve

On Apr 15, 1:36 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
> Ups wait, bool type asks for "true" "false" kind of thing. I wil lfind.
>
> Tuna Toksöz
> Eternal sunshine of the open source mind.
>
> http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitter.com/tehlike
>
> On Wed, Apr 15, 2009 at 8:33 PM, Tuna Toksoz <tehl...@gmail.com> wrote:
> > What is the type in the database? bit?
>
> > Tuna Toksöz
> > Eternal sunshine of the open source mind.
>
> >http://devlicio.us/blogs/tuna_toksoz
> >http://tunatoksoz.com
> >http://twitter.com/tehlike
>

Tuna Toksoz

unread,
Apr 15, 2009, 2:35:26 PM4/15/09
to fluent-n...@googlegroups.com
I think this mapping worked fine

    <class
        name="NHibernate.Test.TypesTest.BooleanClass, NHibernate.Test"
        table="bc_bool"
            discriminator-value="false"
    >
   

            <id name="Id" column="id">
            <generator class="assigned" />
        </id>
            <discriminator column="Osman"
                                         type="Boolean" />
           
           
            <property name="BooleanValue"
                                type="Boolean"
                                column="boolc"/>
   
            <subclass name="NHibernate.Test.TypesTest.BooleanClass2, NHibernate.Test"
                                discriminator-value="true">
            </subclass>

        </class>

but don't know what the fnh produced. it may have to do with the default values you provided (true, false). Do you know what it produces as output?




Tuna Toksöz
Eternal sunshine of the open source mind.

http://devlicio.us/blogs/tuna_toksoz
http://tunatoksoz.com
http://twitter.com/tehlike




Hudson Akridge

unread,
Apr 15, 2009, 3:19:04 PM4/15/09
to fluent-n...@googlegroups.com
Here's essentially what gets produced as output given the OP's class heirarchy:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" namespace="Lib">
  <class name="Comment" table="" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int64" column="">
      <generator class="identity" />
    </id>
    <discriminator column="IsOfficial" type="Boolean" />
    <property name="CreatedAt">
      <column name="CreatedAt" not-null="true" />
    </property>
    <property name="Body">
      <column name="Body" not-null="true" />
    </property>
    <subclass name="Lib.OfficialComment, Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="True" />
    <subclass name="Lib.UserComment, Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="False" />
  </class>
</hibernate-mapping>


Looking at that, the only thing I can think of that's different between Tuna's, is the discriminator value is Title Cased, instead of lower cased for true/false. Unless you guys see something I'm missing...

Tuna Toksoz

unread,
Apr 15, 2009, 3:24:09 PM4/15/09
to fluent-n...@googlegroups.com
I cannot see the reason, and this isn't that hard i am sure. I am missing something obvious but what?

I don't have any other explanation at the moment.

Steven Harman

unread,
Apr 15, 2009, 4:01:31 PM4/15/09
to Fluent NHibernate
The mapping file generated by FNH is:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MyApplication.Model, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" namespace="MyApplication.Model">
<class name="Comment" table="Comment" xmlns="urn:nhibernate-
mapping-2.2">
<id name="ID" type="Int64" column="">
<generator class="identity" />
</id>
<discriminator column="IsOfficial" type="Boolean" />
<property name="Body">
<column name="Body" not-null="true" length="10000" />
</property>
<property name="CreatedAt">
<column name="CreatedAt" not-null="true" />
</property>
<subclass name="MyApplication.Model.UserComment,
MyApplication.Model, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" discriminator-value="False" />
<subclass name="MyApplication.Model.OfficialComment,
MyApplication.Model, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" discriminator-value="True" />
</class>
</hibernate-mapping>

As Hudson said, the only real difference I see between that and what
you've got is the capitalization of the value discriminator-value
attribute. However, I thought that System.Boolean.Parse(String value)
was case-insensitive.

Thoughts?
-steve

On Apr 15, 2:35 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
> I think this mapping worked fine
>
>     <class
>         name="NHibernate.Test.TypesTest.BooleanClass, NHibernate.Test"
>         table="bc_bool"
>             discriminator-value="false"
>     >
>
>             <id name="Id" column="id">
>             <generator class="assigned" />
>         </id>
>             <discriminator column="Osman"
>                                          type="Boolean" />
>
>             <property name="BooleanValue"
>                                 type="Boolean"
>                                 column="boolc"/>
>
>             <subclass name="NHibernate.Test.TypesTest.BooleanClass2,
> NHibernate.Test"
>                                 discriminator-value="true">
>             </subclass>
>
>         </class>
>
> but don't know what the fnh produced. it may have to do with the default
> values you provided (true, false). Do you know what it produces as output?
>
> Tuna Toksöz
> Eternal sunshine of the open source mind.
>
> http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitter.com/tehlike
>
> On Wed, Apr 15, 2009 at 9:02 PM, Steven Harman <stevehar...@gmail.com>wrote:
>
>
>
> > Tuna, thanks for the quick replies... but I'm not sure I understand
> > your last post. Are you saying you think this might be a NHibernate
> > issue with parsing bit fields for bools?
>
> > Thanks,
> > -steve
>
> > On Apr 15, 1:36 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
> > > Ups wait, bool type asks for "true" "false" kind of thing. I wil lfind.
>
> > > Tuna Toksöz
> > > Eternal sunshine of the open source mind.
>
> >http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitt...

Tuna Toksoz

unread,
Apr 15, 2009, 4:02:29 PM4/15/09
to fluent-n...@googlegroups.com
It is case insensitive. I must be missing something.

Hudson Akridge

unread,
Apr 15, 2009, 4:30:06 PM4/15/09
to fluent-n...@googlegroups.com
Steven, could you post your full class data model definition, as well as the statements required to generate your error? I'd like to take a look at this with as close to parity as I can get with you :)
Reply all
Reply to author
Forward
0 new messages