Sequence contains no element

When got this error using NHibernate, it’s likely that your mappings and data models are not registered with the NHibernate configuration object.

Simply add the Assembly that contains the mapping files and the model declaration and it will work.

Also, make sure the mapping files are configured to be embedded resource in the project.

Hibernate mapping for decimal scale is wrong

When mapping a SQL table to a model using Hibernate or NHibernate, if you configure the mapping like following

<property name=”MyDecCol” type=”Decimal” precision=”28″ scale=”15″>
<column name=”MyDecCol” sql-type=”decimal” not-null=”false” />
</property>

For some reason, Hibernate/NHibernate will treat the field as a decimal with scale = 5 (5 decimal digits on the right side of the decimal separator). So if you save a value to this column with more decimal digits, it will be truncated to only 5 decimal digits.

I’m not sure why, but if you move the attributes to the inner column element, Hibernate and NHibernate will map the values correctly.

<property name=”MyDecCol” type=”Decimal”>
<column name=”MyDecCol” sql-type=”decimal” not-null=”false” precision=”28″ scale=”15″ />
</property>

NHibernate: Unable to resolve property: _Property

This issue occurred when

  • There are two entities, same class, same table, different entity names.
  • Both entities have one-to-many relationship to a child table
  • Both one-to-many relationships specify the foreign key column as not-null=”true”
  • Try to call Session.Merge on one entity, with additional new child entities inside.

After trying random configurations, somehow removing not-null=”true” from one entity hbm mapping solves this issue.

No idea why, the only way is, perhaps, to debug NHibernate code.

Update in Jan 2021: Seems that setting inverse = “true” to one of the entities will solve the problem

For inverse = “true” in NHibernate, refer to this link: https://mkyong.com/hibernate/inverse-true-example-and-explanation/

It’s also possible to fix this issue by setting up both one-to-many and many-to-one relationships between 2 entities.

NHibernate, Unable to resolve property _Namespace

This bug was reported and fixed before in earlier versions of NH( https://nhibernate.jira.com/browse/NH-3234 ), but for some reason it resurfaced and hasn’t been fixed since (https://nhibernate.jira.com/browse/NH-3886)

This happens when

  • There is a one-to-many relationship between Parent and Child with cascading.
  • A child has a many-to-one reference to parent in .hbm.xml mapping file.
  • A transient entity of Parent has new transient entities of Child
  • Performing Session.Merge(parent)

The error thrown is in the format: Unable to resolve property: _Namespace. Where namespace is the first part of your namespace in the project.

Somehow removing the many-to-one configuration from the Child entity hbm.xml file will stop NHibernate from throwing this error.

Hopefully it will be fixed in the future versions of NH.

API Reference for NHibernate

While trying to figure out how a method in NHibernate works, I looked for the API Reference and couldn’t find it.

Turns out, NHibernate is a port of Hibernate for .NET framework, so you can find all the API reference in Hibernate API reference.

At least they could have included a link on their website, this probably wasted a lot of everyone’s time.