Nhibernate重复数据是结果即将到来,映射问题

时间:2015-08-06 05:26:03

标签: nhibernate nhibernate-mapping queryover

我在nHibernate中遇到了一个问题。我有一个视图,它在表中有多个记录对应一个通知id。例如 - 对于通知id = 1有多个记录:

下面是我的n hibernate映射类。它继承自Emailnotification base.which具有所有基本属性

以下是该视图的域类:

public partial class vwNotificationPolicyDeliveryCoverLetters : EmailNotificationBase
{
    public vwNotificationPolicyDeliveryCoverLetters()
    {
        //ReasonOfInAccuracy = new List<vwNotificationInaccuracyFailureReasons>();
        EmailNotificationBase = new List<EmailNotificationBase>();
    }


    public virtual long NotificationId
    {
        get;
        set;
    }
    public virtual int? PolicyAccurateLookupId
    {
        get;
        set;
    }
    public virtual string PolicyCheckBy
    {
        get;
        set;
    }

    public virtual string SendOnBehalfOf
    {
        get;
        set;
    }
    public virtual string InAccuracyComments
    {
        get;
        set;
    }
    public virtual DateTime? PolicyCheckDate
    {
        get;
        set;
    }
    public virtual string ReasonOfInAccuracy
    {
        get;
        set;
    }


    public virtual string PolicyNumber
    {
        get;
        set;
    }

    public virtual string PolicyLobCode
    {
        get;
        set;
    }
    public virtual string PolicyDescription
    {
        get;
        set;
    }
    public virtual string PolicyType
    {
        get;
        set;
    }   

    public virtual DateTime? PolicyEffectiveDate
    {
        get;
        set;
    }

    public virtual double? PolicyId
    {
        get;
        set;
    } 

    public virtual string ContactName
    {
        get;
        set;
    }
    public virtual string DeliveryContactEmail
    {
        get;
        set;
    }
    public virtual string SendOnBehalfOfEmailAddress
    {
        get;
        set;
    }
    public virtual long DeliveryContactId
    {
        get;
        set;
    }
    public virtual DateTime? PolicyExpirationDate
    {
        get;
        set;
    }

    public virtual IList<EmailNotificationBase> EmailNotificationBase
    {
        get;
        set;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(this, obj))
            return true;

        return Equals(obj as vwNotificationPolicyDeliveryCoverLetters);
    }

    public virtual bool Equals(vwNotificationPolicyDeliveryCoverLetters obj)
    {
        if (obj == null) return false;

        if (Equals(Action, obj.Action) == false) return false;            
        if (Equals(Comments, obj.Comments) == false) return false;  
        if (Equals(Link, obj.Link) == false) return false;          
        if (Equals(NotificationId, obj.NotificationId) == false) return false;
        if (Equals(NotificationIndicator, obj.NotificationIndicator) == false) return false;
        if (Equals(NotificationStatus, obj.NotificationStatus) == false) return false;
        if (Equals(NotificationType, obj.NotificationType) == false) return false;          
        if (Equals(RequestState, obj.RequestState) == false) return false;
        if (Equals(ServiceRequestId, obj.ServiceRequestId) == false) return false;
        if (Equals(ServiceRequestNumber, obj.ServiceRequestNumber) == false) return false;          
        if (Equals(ClientName, obj.ClientName) == false) return false;
        if (Equals(ClientNumber, obj.ClientNumber) == false) return false;
        if (Equals(PolicyId, obj.PolicyId) == false) return false;
        if (Equals(PolicyNumber, obj.PolicyNumber) == false) return false;
        if (Equals(PolicyLobCode, obj.PolicyLobCode) == false) return false;
        if (Equals(PolicyDescription, obj.PolicyDescription) == false) return false;
        if (Equals(PolicyType, obj.PolicyType) == false) return false;
        if (Equals(ServicingLocation, obj.ServicingLocation) == false) return false;
        if (Equals(PolicyEffectiveDate, obj.PolicyEffectiveDate) == false) return false;
        if (Equals(ContactName, obj.ContactName) == false) return false;
        if (Equals(DeliveryContactEmail, obj.DeliveryContactEmail) == false) return false;
        if (Equals(SendOnBehalfOfEmailAddress, obj.SendOnBehalfOfEmailAddress) == false) return false;
        if (Equals(DeliveryContactId, obj.DeliveryContactId) == false) return false;
        if (Equals(PolicyExpirationDate, obj.PolicyExpirationDate) == false) return false;
        if (Equals(PolicyAccurateLookupId, obj.PolicyAccurateLookupId) == false) return false;
        if (Equals(PolicyCheckBy, obj.PolicyCheckBy) == false) return false;
        if (Equals(PolicyCheckDate, obj.PolicyCheckDate) == false) return false;

        if (Equals(ReasonOfInAccuracy, obj.ReasonOfInAccuracy) == false) return false;
        if (Equals(InAccuracyComments, obj.InAccuracyComments) == false) return false;
        if (Equals(SendOnBehalfOf, obj.SendOnBehalfOf) == false) return false;

        return true;
    }

    public override int GetHashCode()
    {
        int result = 1;

        result = (result * 397) ^ (Action != null ? Action.GetHashCode() : 0);            
        result = (result * 397) ^ (Comments != null ? Comments.GetHashCode() : 0);
        result = (result * 397) ^ (Link != null ? Link.GetHashCode() : 0);          
        result = (result * 397) ^ NotificationId.GetHashCode();
        result = (result * 397) ^ (NotificationIndicator != null ? NotificationIndicator.GetHashCode() : 0);
        result = (result * 397) ^ (NotificationStatus != null ? NotificationStatus.GetHashCode() : 0);
        result = (result * 397) ^ (NotificationType != null ? NotificationType.GetHashCode() : 0);          
        result = (result * 397) ^ (RequestState != null ? RequestState.GetHashCode() : 0);
        result = (result * 397) ^ (ServiceRequestId != null ? ServiceRequestId.GetHashCode() : 0);
        result = (result * 397) ^ (ServiceRequestNumber != null ? ServiceRequestNumber.GetHashCode() : 0);          
        result = (result * 397) ^ (ClientName != null ? ClientName.GetHashCode() : 0);
        result = (result * 397) ^ (ClientNumber != null ? ClientNumber.GetHashCode() : 0);
        result = (result * 397) ^ (PolicyId != null ? PolicyId.GetHashCode() : 0);
        result = (result * 397) ^ (PolicyNumber != null ? PolicyNumber.GetHashCode() : 0);
        result = (result * 397) ^ (PolicyLobCode != null ? PolicyLobCode.GetHashCode() : 0);
        result = (result * 397) ^ (PolicyDescription != null ? PolicyDescription.GetHashCode() : 0);
        result = (result * 397) ^ (PolicyType != null ? PolicyType.GetHashCode() : 0);
        result = (result * 397) ^ (ServicingLocation != null ? ServicingLocation.GetHashCode() : 0);
        result = (result * 397) ^ (PolicyEffectiveDate != null ? PolicyEffectiveDate.GetHashCode() : 0);
        result = (result * 397) ^ (ContactName != null ? ContactName.GetHashCode() : 0);
        result = (result * 397) ^ (DeliveryContactEmail != null ? DeliveryContactEmail.GetHashCode() : 0);
        result = (result * 397) ^ (SendOnBehalfOfEmailAddress != null ? SendOnBehalfOfEmailAddress.GetHashCode() : 0);
        result = (result * 397) ^ DeliveryContactId.GetHashCode();
        result = (result * 397) ^ (PolicyAccurateLookupId != null ? PolicyAccurateLookupId.GetHashCode() : 0);
        result = (result * 397) ^ (PolicyCheckBy != null ? PolicyCheckBy.GetHashCode() : 0);
        result = (result * 397) ^ (PolicyCheckDate != null ? PolicyCheckDate.GetHashCode() : 0);
        result = (result * 397) ^ (ReasonOfInAccuracy != null ? ReasonOfInAccuracy.GetHashCode() : 0);
        result = (result * 397) ^ (PolicyExpirationDate != null ? PolicyExpirationDate.GetHashCode() : 0);
        result = (result * 397) ^ (InAccuracyComments != null ? InAccuracyComments.GetHashCode() : 0);
        result = (result * 397) ^ (SendOnBehalfOf != null ? SendOnBehalfOf.GetHashCode() : 0);

        return result;
    }
}
}

下面是基类的hbm文件。内部已经为子类创建了union-sub类:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Aon.ATD.Domain.Model" assembly="Aon.ATD.DomainModel" xmlns="urn:nhibernate-mapping-2.2">
<class name="EmailNotificationBase" abstract="true">
    <id name="NotificationId" access="property" column="`NotificationId`">
        <generator class="assigned" />
    </id>
<property name="ServiceRequestId" column="`ServiceRequestId`" />
<property name="ServiceRequestNumber" column="`ServiceRequestNumber`" />
<property name="ClientName" column="`ClientName`" />
<property name="ClientNumber" column="`ClientNumber`" />
<property name="AttachmentGroupId" column="`AttachmentGroupId`" />
<property name="Comments" column="`Comments`" />
<property name="NotificationType" column="`NotificationType`" />
<property name="NotificationStatus" column="`NotificationStatus`" />    
    <property name="Action" column="`Action`" />
<property name="Link" column="`Link`" />
<property name="NotificationIndicator" column="`Notification Indicator`" />
<property name="RequestState" column="`RequestState`" />
<property name="NotificationTypeId" column="`NotificationTypeId`" />
<property name="DistributionTypeId" column="`DistributionTypeId`" />
<property name="DistributionType" column="`DistributionType`" />
<property name="Office" column="`Office`" />
<property name="ModifiedDateTime" column="`ModifiedDateTime`" />
<property name="ServicingLocation" column="`ServicingLocation`" />
<property name="RequestCategoryId" column="`RequestCategoryId`" />
<property name="RequestTypeId" column="`RequestTypeId`" />

<many-to-one name="AttachmentGroup" class="AttachmentGroup" column="`AttachmentGroupId`" not-null="false" fetch="select" lazy="false" />
<!--<bag name="PolicyDeliveryCoverLetters" inverse="true"  >
  <key column="`NotificationId`" />
  <one-to-many class="vwNotificationPolicyDeliveryCoverLetters" />
</bag>-->

<union-subclass name="VwEmailNotification" table="`VwEmailNotification`" />
    <union-subclass name="VwNotificationAudit" table="`vwNotificationAudit`">
  <property name="PolicyNumber" column="`PolicyNumber`" />
  <property name="LobCode" column="`LobCode`" />
  <property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
        <property name="PolicyId" column="`PolicyId`" />
  <property name="CarrierName" column="`CarrierName`" />
  <property name="DocumentType" column="`DocumentType`" />

  <!--
  <bag name="PITDSRsByPolicyID" inverse="true" lazy="false">
    <key column="`PolicyId`" />
    <one-to-many class="VwPITDSRByPolicyId" />
  </bag>
  -->

    </union-subclass>
<union-subclass name="VwNotificationEndorsement" table="`vwNotificationEndorsement`">
  <property name="PolicyNumber" column="`PolicyNumber`" />
  <property name="LobCode" column="`LobCode`" />
  <property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
  <property name="PolicyId" column="`PolicyId`" />
  <property name="CarrierName" column="`CarrierName`" />
</union-subclass>
<union-subclass name="VwNotificationPolicy" table="`vwNotificationPolicy`">
  <property name="PolicyNumber" column="`PolicyNumber`" />
  <property name="LobCode" column="`LobCode`" />
  <property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
  <property name="PolicyId" column="`PolicyId`" />
  <property name="CarrierName" column="`CarrierName`" />
  <property name="DocumentType" column="`DocumentType`" />

  <!--
  <bag name="PITDSRsByPolicyID" inverse="true" lazy="false">
    <key column="`PolicyId`" />
    <one-to-many class="VwPITDSRByPolicyId" />
  </bag>
  -->

</union-subclass>
<union-subclass name="VwNotificationNotARSAccount" table="`vwNotificationNotARSAccount`">
  <property name="PolicyNumber" column="`PolicyNumber`" />
  <property name="LobCode" column="`LobCode`" />
  <property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
  <property name="PolicyId" column="`PolicyID`" />
  <property name="CarrierName" column="`CarrierName`" />
  <property name="EmailAddress" column="`EmailAddress`" />
</union-subclass>
<union-subclass name="VwNotificationOfficePend" table="`vwNotificationOfficePend`">
  <property name="PolicyNumber" column="`PolicyNumber`" />
  <property name="LobCode" column="`LobCode`" />
  <property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
  <property name="EmailAddress" column="`EmailAddress`" />
</union-subclass>
<union-subclass name="VwNotificationNotAONServiced" table="`vwNotificationNotAONServiced`">
  <property name="PolicyNumber" column="`PolicyNumber`" />
  <property name="LobCode" column="`LobCode`" />     
  <property name="CarrierName" column="`CarrierName`" />
  <property name="CarrierGroupCode" column="`CarrierGroupCode`" />
</union-subclass>
<union-subclass name="VwNotificationDocumentOutOfScope" table="`vwNotificationDocumentOutOfScope`">
  <property name="PolicyNumber" column="`PolicyNumber`" />
  <property name="LobCode" column="`LobCode`" />
  <property name="CarrierName" column="`CarrierName`" />
  <property name="DocumentType" column="`DocumentType`" />
  <property name="CarrierGroupCode" column="`CarrierGroupCode`" />
</union-subclass>
<union-subclass name="VwNotificationDiscrepancy" table="`vwNotificationDiscrepancy`">
  <property name="Astindicator" column="`Astindicator`" />    
  <property name="PolicyId" column="`PolicyId`" />    
</union-subclass>
<union-subclass name="VwNotificationPolicyDelivery" table="`vwNotificationPolicyDelivery`">
  <property name="PolicyNumber" column="`PolicyNumber`" />
  <property name="PolicyLobCode" column="`PolicyLobCode`" />
  <property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
  <property name="PolicyId" column="`PolicyID`" />
  <property name="PolicyDescription" column="`PolicyDescription`" />
  <property name="PolicyType" column="`PolicyType`" />
   </union-subclass>

<union-subclass name="vwNotificationPolicyDeliveryCoverLetters" table="`vwNotificationPolicyDeliveryCoverLetters`">
  <property name="PolicyNumber" column="`PolicyNumber`" />
  <property name="PolicyLobCode" column="`PolicyLobCode`" />
  <property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
  <property name="PolicyId" column="`PolicyID`" />
  <property name="PolicyDescription" column="`PolicyDescription`" />
  <property name="PolicyType" column="`PolicyType`" />
  <property name="ContactName" column="`ContactName`" />
  <property name="DeliveryContactEmail" column="`DeliveryContactEmail`" />
  <property name="SendOnBehalfOfEmailAddress" column="`SendOnBehalfOfEmailAddress`" />
  <property name="DeliveryContactId" column="`DeliveryContactId`" />
  <property name="PolicyExpirationDate" column="`PolicyExpirationDate`" />
  <property name="PolicyAccurateLookupId" type="Int32" column="`PolicyAccurateLookupId`" />
  <property name="PolicyCheckBy" type="String" column="`PolicyCheckBy`" />
  <property name="PolicyCheckDate" type="DateTime" column="`PolicyCheckDate`" />
  <property name="ReasonOfInAccuracy" column="`ReasonOfInAccuracy`" />
  <property name="InAccuracyComments" column="`InAccuracyComments`" />
  <property name="SendOnBehalfOf" column="`SendOnBehalfOf`" />
  <property name="NotificationId" column="`NotificationId`" />
</union-subclass>

                                                                                                                                  

获取对应于通知id的第一行记录时获取重复。例如,对于通知id = 1,有三行。在获取记录时,第二行和第三行的数据与第一行相同。

1 个答案:

答案 0 :(得分:0)

这是预期的行为。 NHibernate看到第二个和第三个记录具有相同的id,因此数据必须相同(id = identity)。它只是将对已创建的第一个对象的引用分发出来。

你真正拥有的是值对象的一对多关联,而不是联合子类,因此将其建模为:

class vwNotificationPolicyDeliveryCoverLetters : EmailNotificationBase
{
    public virtual Notification Owner { get; set; }

    //other properties
}

<class name="VwEmailNotification" table="`VwEmailNotification`" />
  <composite-id>
    <key-many-to-one name="Owner" column="`notification_id`" />
    <key-property name="PolicyNumber" column="`PolicyNumber`" />
    <key-property name="LobCode" column="`LobCode`" />
    <key-property name="PolicyEffectiveDate" column="`PolicyEffectiveDate`" />
    <key-property name="PolicyId" column="`PolicyId`" />
    <key-property name="CarrierName" column="`CarrierName`" />
    <key-property name="DocumentType" column="`DocumentType`" />
  </composite-id>
</class>

所有属性上的compositeid都与您的同等实现相匹配