0

我知道当数据库中映射组件的所有属性都为空时,NHibernate 会将组件设置为空。但是,在以下情况下,如果多对一项目LevelOfInvolvement为空,则该组件被设置为空,尽管该组件的所有其他属性都不为空。

无论其他字段的值如何,更改数据库中LevelOfInvolvement列的值都会导致组件为空或不为空。

这是完整的映射:

<?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>

加载实体的代码很简单:

entity = NHibernateSession.Load(persitentType, id);

我正在使用 NHibernate 版本 3.3.1

这似乎是一个相对简单的映射,但我找不到其他有类似问题的参考。

4

1 回答 1

3

除了在所有组件成员为 null 时返回 null 之外,我相信 NHibernate 还会在任何不可为 null 的成员为 null 时将组件视为 null。默认情况下,<many-to-one>映射会创建一个不可为空的成员,我认为这会导致您看到的行为。尝试更改LevelOfInvolement映射以允许空值:

<many-to-one 
    name="LevelOfInvolvement" 
    column="InvolvementChecklist_LevelOfInvolvementID" 
    not-null="false" />
于 2012-07-19T22:14:05.917 回答