Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1106933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:51:51+00:00 2026-05-17T01:51:51+00:00

I am trying to use NHibernate to map a legacy database which uses a

  • 0

I am trying to use NHibernate to map a legacy database which uses a decimal(9,0) (~32 bit integer) “version property” (for optimistic locking of each row).

The fields are declared nullable in the database, and in section 5.1.7 of the NHibernate reference manual it states:

Version numbers may be of type Int64, Int32, Int16, Ticks, Timestamp, or TimeSpan (or their nullable counterparts
in .NET 2.0).

… so I mapped them as int? version properties.

My mapping document looks similar to this:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Model" assembly="Model" xmlns="urn:nhibernate-mapping-2.2">
    <class name="Bar" lazy="true" table="`BAR`" schema="`dbo`">
        <id name="ID" access="property" column="`ID`">
            <generator class="assigned" />
        </id>
        <version name="Version" column="`VERSION`" type="int?" />
        <!-- ... etc ... -->
    </class>
</hibernate-mapping>

However, at runtime, this fails miserably with an exception similar to:

failed: NHibernate.MappingException : Could not compile the mapping document: Model.Mappings.Bar.hbm.xml
  ----> NHibernate.MappingException : Could not determine type for: Model.int?,  for columns: NHibernate.Mapping.Column(VERSION)
    at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
    at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
    at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
    at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
    at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
    at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
    at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
    at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
    Core\DAOUtils.cs(33,0): at Core.DAOUtils.OpenSession()
    test\cs\FooTest.cs(65,0): at DataModel.Tests.FooTest.TestSelectStar()
    --MappingException
    at NHibernate.Mapping.SimpleValue.get_Type()
    at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.BindProperty(HbmVersion versionSchema, Property property, IDictionary`2 inheritedMetas)
    at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.BindVersion(HbmVersion versionSchema, PersistentClass rootClass, Table table, IDictionary`2 inheritedMetas)
    at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(XmlNode node, HbmClass classSchema, IDictionary`2 inheritedMetas)
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode parentNode, IDictionary`2 inheritedMetas)
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(XmlNode node)
    at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)

Is there some trick to correctly using version properties with NHibernate, or am I doing something wrong?

One odd thing to me is that NHibernate seems to be appending my assembly/namespace name to the beginning of the int? type, which seems wrong.

Edit:

After changing the mapping files to use int instead of int? I am now hitting the following exception:

System.InvalidCastException : Unable to cast object of type 'NHibernate.Type.DecimalType' to type 'NHibernate.Type.IVersionType'.
    at NHibernate.Tuple.PropertyFactory.BuildVersionProperty(Property property, Boolean lazyAvailable)
    at NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass, ISessionFactoryImplementor sessionFactory)
    at NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory)
    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()

Unfortunately, NHibernate isn’t giving me a very detailed error message here (it would be nice to know which class out of the hundreds of classes is throwing this), and I haven’t been able to convince log4net to work, so I’m at a loss for the moment.

Edit #2:

To summarize the “tricks”:

  • Assuming integer version properties, (and even if they are declared decimal in the database) make sure all version properties are declared as int in the mapping XML. (though it should work if int? is declared in the POCO)
  • Make sure no version properties are set as a decimal type (either in the POCOs or the mapping XML)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:51:52+00:00Added an answer on May 17, 2026 at 1:51 am

    You shouldn’t include the ‘?’ in the mapping – instead write type=”Int32″. If your property in your POCO is nullable it should work out fine. (At least it does for properties – I haven’t tried having a nullable version column…).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use NHibernate for data access, and I have 2 simple entities
Trying to use an excpetion class which could provide location reference for XML parsing,
I'm trying to use svnmerge.py to merge some files. Under the hood it uses
Background: I'm getting a mapping failure when trying to use nHibernate. The application consists
I've been trying to use #harp architecture and Fluent-NHibernate. I am trying to sublass
There must be a simple solution out there, I'm trying to use NHibernate Validator
How to get Hibernate session inside a Hibernate Interceptor? I'm trying to use Hibernate
Trying to use a guid as a resource id in a rest url but
While trying to use LINQ to SQL I encountered several problems. I have table
I' trying to use a Linq query to find and set the selected value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.