0
votes

I understand that when all the properties of a mapped component are null in the database, NHibernate will set the component to null. However, in the following case the component is being set to null if the many-to-one item LevelOfInvolvement is null, despite the fact that all the other properties of the component are not null.

Changing the value of the LevelOfInvolvement column in the database will cause the component to be null or not, regardless of the value of the other fields.

Here is the maping in full:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHS.WebTeam.LPTInvolvementHub.Activity, NHS.WebTeam.LPTInvolvementHub" table="[Activity]" lazy="false" optimistic-lock="version" where="Deleted=0" >
    <id name="ID" type="Int32" column="ID" unsaved-value="0">
      <generator class="hilo">
        <param name="table">NHibernateHiLo</param>
        <param name="column">NextValue</param>
        <param name="max_lo">100</param>
      </generator>
    </id>

    <version name="Version"/>
    <property name="Deleted" />
    <property name="LastUpdateBy" />
    <property name="LastUpdateDate" />

    <many-to-one name="Service" column="ServiceID"></many-to-one>
    <property name="Title"></property>
    <property name="Abstract"></property>
    <property name="ProposedDate"></property>
    <property name="DueDate"></property>
    <property name="ActualDate"></property>
    <property name="Rationale"></property>
    <many-to-one name="PreAssessment" column="PreAssessmentID"></many-to-one>
    <many-to-one name="PostAssessment" column="PostAssessmentID"></many-to-one>

    <component name="InvolvementChecklist">
      <property name="Impact" column="InvolvementChecklist_Impact"></property>
      <property name="RewardsAndRecognition" column="InvolvementChecklist_RewardsAndRecognition"></property>
      <property name="Training" column="InvolvementChecklist_Training"></property>
      <property name="LogisticalIssues" column="InvolvementChecklist_LogisticalIssues"></property>
      <property name="Feedback" column="InvolvementChecklist_Feedback"></property>
      <property name="DueRegard" column="InvolvementChecklist_DueRegard"></property>
      <property name="SupportRequiredFromTheTeam" column="InvolvementChecklist_SupportRequiredFromTheTeam"></property>
      <property name="QualityAssurance" column="InvolvementChecklist_QualityAssurance"></property>
      <property name="NonComplianceReason" column="InvolvementChecklist_NonComplianceReason"></property>
      <many-to-one name="LevelOfInvolvement" column="InvolvementChecklist_LevelOfInvolvementID"></many-to-one>
    </component>

  </class>
</hibernate-mapping>

The code to load the entity is simply:

entity = NHibernateSession.Load(persitentType, id);

I am using NHibernate version 3.3.1

This seems a relatively simple mapping, but I cannot find any reference to others having similar problems.

1
do you have nhibernate profiler to see the translated SQL to test what it returns?Alexandros B
I don't have NHibernate Profiler, but can get the SQL via Log4Net. Running the query returns the expected values for the component.Richard Bramley

1 Answers

3
votes

In addition to returning null if all of the component members are null, I believe NHibernate will also treat a component as null if any non-nullable members are null. By default a <many-to-one> mapping creates a non-nullable member, which I think would cause the behaviour you're seeing. Try changing the LevelOfInvolement mapping to allow nulls:

<many-to-one 
    name="LevelOfInvolvement" 
    column="InvolvementChecklist_LevelOfInvolvementID" 
    not-null="false" />