NHibernate component mapping – Null Component


I have a mapped entity, Matter, that has a mapped component, Injury.

The only property on the Injury is DateOfInjury which is a nullable datetime.

When I retrieve the Matter, if the DateOfInjury is null, the component is null.

Thus something like this matter.Injury.DateOfInjury will throw.

Could someone explain if I am doing something obvious to cause this behaviour?

I would have expected that the Injury component gets initialized by nHibernate as an object and that the DateOfinjury property is null.

This would be more flexible i would think?

Best Solution

I think that's the default behavior for a component mapping. The NHibernate docs for component say that if all elements of the component are null, the component itself will just be null.

If you only have a single property in the component, it might make sense to just map it as a nullable DateTime property on the Matter class.

Related Question