复合键未尝试保留键值



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

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

。但是在尝试将记录合并到表中之前,我已经指定了值。 使用断点,我能够在提交会话事务之前验证对象是否确实具有值。

我做错了什么? 我需要指定键的值。

我不

记得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 >%
}

相关内容

  • 没有找到相关文章

最新更新