2

好吧,我又在挠头了。我有这样的课:

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 回答