NHibernate.MappingException occurs when using nullable types

2.2k views Asked by At

I am using nhibernate to access my database and andromda generates my mapping files. All is working fine, as long as I am not using nullable datatypes. What I am trying is to have an entity with a property of type Nullables.NHibernate.NullableInt32Type. My database has the corresponding relation with the column of type "int NULL" (SQL Server). The corresponding class has the correct data type, too (int?). But when I try to get values of the database, I get an NHibernate.MappingException:

NHibernate.MappingException : Invalid mapping information specified for type Namespace.SummaryAttribute, check your mapping file for property type mismatches ----> System.InvalidCastException : Die angegebene Umwandlung ist ungültig.

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping
    xmlns="urn:nhibernate-mapping-2.2"
    default-cascade="none">

    <class
        name="Namespace.SummaryAttribute, Core"
        table="SUMMARY_ATTRIBUTE"
        dynamic-insert="false"
        dynamic-update="false"
        lazy="true">

        <id name="Id" type="Int64" unsaved-value="0">
            <column name="ID" sql-type="NUMERIC(19,0)"/>
            <generator class="native">
            </generator>
        </id>

        <property name="ShortName" type="String">
            <column name="SHORT_NAME" not-null="true" unique="false" sql-type="VARCHAR(255)"/>
        </property>

        <property name="LongName" type="String">
            <column name="LONG_NAME" not-null="true" unique="false" sql-type="VARCHAR(255)"/>
        </property>

        <property name="Description" type="String">
            <column name="DESCRIPTION" not-null="true" unique="false" sql-type="VARCHAR(255)"/>
        </property>

        <property name="IsVisible" type="Boolean">
            <column name="IS_VISIBLE" not-null="true" unique="false" sql-type="BIT"/>
        </property>

        <property name="DecimalPlaces" type="Nullables.NHibernate.NullableInt32Type, Nullables.NHibernate">
            <column name="DECIMAL_PLACES" not-null="false" unique="false" sql-type="INT"/>
        </property>
    </class>
</hibernate-mapping>

Without the property "DecimalPlaces" all works fine. Even when I change the property to the simple data type int.

Does anyone have a clue what the problem might be?

1

There are 1 answers

1
Sly On BEST ANSWER

Just remove type property from mappings. NHibernate will figure out type by himself, so you should have:

<property name="DecimalPlaces">
    <column name="DECIMAL_PLACES" not-null="false" unique="false" sql-type="INT"/>
</property>