0

我有一个带有 5 个域对象的棕地应用程序:CreditMemo、CreditMemoDetail、SalesInvoice、ReceivablesRecord、TransactionType。CreditMemo 是一个--多个 CreditMemoDetail。它也与 SalesInvoice 是多对一的。SalesInvoice 与 ReceivablesRecord 是一对多的,而 ReceivablesRecord 与 TransactionType 是多对一的(下面的 xml 映射)。没有任何关系依赖于其直接父/子之外的任何值。

当我执行 Get(id) 时,我正确地恢复了整个结构:CreditMemo 中的列已正确填写,ISet 正确,并且整个 SalesInvoice/ReceivablesRecord/TransactionType 层次结构已完全填充。当我执行 Get(invoiceNumber) 时,SalesInvoice 对象已正确填写,但 ISet 为空。使用 NHProfiler,执行完全相同的查询,包括参数。我已经敲了这个几个小时了,还没有想出任何有意义的东西。

我知道我可以强行处理事情,但是当从 CreditMemo 开始时,关系会自动运行,但在从 SalesInvoice 开始时却不能,这一事实让我发疯了。有任何想法吗?

对象和映射:

public class CreditMemo
{
    public virtual int CreditMemoId { get; set; }
    public virtual string CreditMemoNumber { get; set; }
    public virtual DateTime CreditDate { get; set; }
    public virtual string InvoiceNumber { get; set; }
    public virtual string ReturnToStock { get; set; }
    public virtual string Posted { get; set; }
    public virtual string Notes { get; set; }
    public virtual Iesi.Collections.Generic.ISet<CreditMemoDetail> Details { get; set; }
    public virtual SalesInvoice InvoiceInfo { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" schema="aladdin" namespace="Receivables.Models">
  <class name="Receivables.Models.CreditMemo, Receivables" lazy="false" table="credit_memo">
    <id name="CreditMemoId" column="credit_memo_id">
      <generator class="identity" />
    </id>
    <property name="InvoiceNumber" column="invoice_no" />
    <property name="CreditMemoNumber" column="credit_memo_no" />
    <property name="CreditDate" column="credit_date" type="DateTime" />
    <property name="ReturnToStock" column="return_to_stock" />
    <property name="Posted" column="posted" />
    <property name="Notes" column="notes" />
    <many-to-one name="InvoiceInfo" class="SalesInvoice" column="invoice_no" update="false" insert="false" />
    <set name="Details" cascade="delete">
      <key column="credit_memo_id" />
      <one-to-many class="CreditMemoDetail"/>
    </set>
  </class>
</hibernate-mapping>

public class CreditMemoDetail
{
    public virtual int CreditMemoDetailId { get; set; }
    public virtual int CreditMemoId { get; set; }
    public virtual string CreditType { get; set; }
    public virtual decimal Amount { get; set; }
    public virtual string AffectCommission { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" schema="aladdin" namespace="Receivables.Models">
  <class name="Receivables.Models.CreditMemoDetail, Receivables" lazy="false" table="cr_memo_detail">
    <id name="CreditMemoDetailId" column="crmemodetailid">
      <generator class="identity" />
    </id>
    <property name="CreditMemoId" column="credit_memo_id" />
    <property name="CreditType" column="credit_type" />
    <property name="Amount" column="amount" />
    <property name="AffectCommission" column="affect_commission" />
  </class>
</hibernate-mapping>

public class SalesInvoice
{
    public virtual string InvoiceNumber { get; set; }
    public virtual string Customer { get; set; }
    public virtual DateTime InvoiceDate { get; set; }
    public virtual string SalesPerson { get; set; }
    public virtual string ClassOfSale { get; set; }
    public virtual Iesi.Collections.Generic.ISet<ReceivablesRecord> Transactions { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" schema="aladdin" namespace="Receivables.Models">
  <class name="Receivables.Models.SalesInvoice, Receivables" lazy="false" table="invoice">
    <id name="InvoiceNumber" column="invoice_no">
      <generator class="assigned" />
    </id>
    <property name="Customer" column="customer" />
    <property name="InvoiceDate" column="invoice_date" type="DateTime" />
    <property name="SalesPerson" column="salesman" />
    <property name="ClassOfSale" column="class" />
    <set name="Transactions">
      <key column="invoice_no" />
      <one-to-many class="ReceivablesRecord"/>
    </set>
  </class>
</hibernate-mapping>

public class ReceivablesRecord
{
    public virtual string InvoiceNumber { get; set; }
    public virtual DateTime AccrecDate { get; set; }
    public virtual string TxType { get; set; }
    public virtual decimal Amount { get; set; }
    public virtual string Note { get; set; }
    public virtual TransactionType TypeInfo { get; set; }
    public override bool Equals(object obj)
    {
        bool result = false;
        try
        {
            ReceivablesRecord input = (ReceivablesRecord)obj;
            if (InvoiceNumber == input.InvoiceNumber && AccrecDate == input.AccrecDate && TxType == input.TxType)
            {
                result = true;
            }
        }
        catch (Exception ex)
        {
        }

        return result;
    }

    public override int GetHashCode()
    {
        return string.Format("{0}{1}{2}", InvoiceNumber, AccrecDate, TxType).GetHashCode();
    }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" schema="aladdin" namespace="Receivables.Models">
  <class name="Receivables.Models.ReceivablesRecord, Receivables" lazy="false" table="accrec">
    <composite-id>
      <key-property name="InvoiceNumber" column="invoice_no" />
      <key-property name="AccrecDate" column="accrec_date" type="DateTime" />
      <key-property name="TxType" column="tx_type" />
    </composite-id>
    <property name="Amount" column="amount" />
    <property name="Note" column="note" />
    <many-to-one name="TypeInfo" class="TransactionType" column="tx_type" />
  </class>
</hibernate-mapping>

public class TransactionType
{
    public virtual string TransactionCode { get; set; }
    public virtual string Description { get; set; }
    public virtual decimal GLAccountNumber { get; set; }
    public virtual string Taxable { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" schema="aladdin" namespace="Receivables.Models">
  <class name="Receivables.Models.TransactionType, Receivables" lazy="false" table="txtype">
    <id name="TransactionCode" column="tx_code">
      <generator class="assigned" />
    </id>
    <property name="Description" column="description" />
    <property name="GLAccountNumber" column="gl_account_no" type="Decimal" />
    <property name="Taxable" column="taxable" />
  </class>
</hibernate-mapping>

存储库代码:

    public CreditMemo GetById(int id)
    {
        return NhSession.Get<CreditMemo>(id);
    }

    public SalesInvoice GetInvoiceByInvoiceNumber(string id)
    {
        SalesInvoice salesInvoice = NhSession.Get<SalesInvoice>(id);
        return salesInvoice;
    }

备用 GetInvoiceByInvoiceNumber确实有效(即,它返回 SalesInvoice 下面的所有详细级别,而不更改映射):

    return NhSession.CreateQuery("from SalesInvoice where InvoiceNumber = :invoice_no")
        .SetString("invoice_no", id).UniqueResult<SalesInvoice>();
4

1 回答 1

0

ReceivablesRecord 的 Equals 方法可能是问题,将其更改为

public override bool Equals(object obj)
{
    ReceivablesRecord other = obj as ReceivablesRecord;
    return other != null &&
           InvoiceNumber == input.InvoiceNumber &&
           AccrecDate == input.AccrecDate &&
           TxType == input.TxType;
}
于 2012-10-26T10:20:16.727 回答