2

OK、もう一度頭をかいてます。私はそのようなクラスを持っています:

public class ReconciliationReportLineItem
{
    public virtual int ID { get; set; }
    public virtual int Category { get; set; }
    public virtual string FileName { get; set; }
    public virtual decimal Amount { get; set; }
    public virtual string BatchType { get; set; }
    public virtual DateTime CreatedTimeStamp { get; set; }
    public virtual string Currency { get; set; }
    public virtual decimal LocalAmount { get; set; }
    public virtual int NumberOfInvoices { get; set; }

    public override bool Equals(object obj)
    {
        var t = obj as ReconciliationReportLineItem;
        if (t == null) return false;
        return
            t.ID == this.ID
            && t.Category == this.Category;
    }

    public override int GetHashCode()
    {
        return string.Format("{0}{1}{2:yyyyMMddHHmmss}{3}{4}{5}{6}",
            this.FileName, this.BatchType, this.CreatedTimeStamp,
            this.NumberOfInvoices, this.LocalAmount, this.Amount,
            this.Currency).GetHashCode();
    }
}

そして私の流暢なマッピングファイルそれ自体:

public class ReconciliationReportLineItemMapping : ClassMap<ReconciliationReportLineItem>
{
    public ReconciliationReportLineItemMapping()
    {
        Table("ReconciliationReportLineItem");

        CompositeId()
            .KeyProperty(x => x.ID, "id")
            .KeyProperty(x => x.Category, "category");

        Map(x => x.FileName)
            .Length(500)
            .Nullable()
            .Index("ixDatroseReconciliationReportLineItemFileName");

        Map(x => x.Amount)
            .Not.Nullable();

        Map(x => x.BatchType)
            .Not.Nullable();

        Map(x => x.CreatedTimeStamp)
            .Index("ixDatroseReconciliationReportLineItemCreatedTimeStamp")
            .Not.Nullable();

        Map(x => x.Currency)
            .Not.Nullable();

        Map(x => x.LocalAmount)
            .Not.Nullable();

        Map(x => x.NumberOfInvoices)
            .Not.Nullable();
    }
}

オブジェクトにデータを入力してコミットしようとすると、次のようなエラーが発生します。

ERROR: 23505: duplicate key value violates unique constraint "reconciliationreportlineitem_pkey"

エラーsqlは、複合キーのメンバー(事前設定されている)がゼロに設定されていることを示しています。

INSERT INTO ReconciliationReportLineItem (FileName, Amount, BatchType, CreatedTimeStamp, Currency, LocalAmount, NumberOfInvoices, id, category) 
VALUES (((NULL)::text), ((E'1065.47')::numeric), ((E'X200 batch created 20121027')::text), ((E'2012-10-27 08:39:00.000000')::timestamp), ((E'USD')::text), ((E'1065.47')::numeric), ((7)::int4), ((0)::int4), ((0)::int4))

...しかし、レコードをテーブルにマージしようとする前に、値を指定していました。ブレークポイントを使用すると、セッショントランザクションをコミットする前に、オブジェクトに実際に値があることを確認できました。

私は何が間違っているのですか?キーの値を指定する必要があります。

4

1 に答える 1

2

NHibernateのデフォルトのキージェネレーターが何であるかは覚えていませんが、これを追加して、コンポーネントを識別子として使用してキーを割り当てることをNHに伝えてみてください。このアプローチの例は多くありませんが、このフォーラムの投稿は部分的なものです。更新されたコードは次のとおりです。

// snipped >%

CompositeId()
    .ComponentCompositeIdentifier<ReconciliationReportLineItemKey>
       (rrli => rrli.Key)
    .KeyProperty(k => k.Key.Id)
    .KeyProperty(k => k.Key.Category);

// snipped >%

次の割り当てを行うには、キーの新しいクラスを追加する必要があります。

public class ReconciliationReportLineItemKey
{
    public virtual int Id { get; set; }
    public virtual int Category { get; set; }
}

また、コンポーネントのプロパティをエンティティクラスに追加します。

public class ReconciliationReportLineItem
{
    // snipped >%

    public virtual ReconciliationReportLineItemKey Key { get; set; }

    // snipped >%
}
于 2012-11-26T19:08:35.697 に答える