自我与许多人的问题加入Fluent NHibernate

时间:2011-03-11 12:39:39

标签: c# fluent-nhibernate

我是一个完整的流利新手,虽然我在'关于多对多连接的网上找到了很多,但我在目前的情况下找不到多少。

注意:好吧,这可能是垃圾设计,我可能会更好地制作单独的表格,对设计的侮辱感谢:)

我有一张这样的表:

Database

等课程:

[Serializable]
public class Transaction : Entity
{
    [DomainSignatureAttribute]
    public virtual long TransactionId { get; protected internal set; }
    public virtual long AccountId { get; set; }
    protected internal virtual short TransactionTypeId { get; set; }
    protected internal virtual short TransactionStatusId { get; set; }
    public virtual DateTime DateCreated { get; set; }
    public virtual DateTime DateEffective { get; set; }
    public virtual Decimal Amount { get; set; }

    public virtual IList<TransactionLink> ChildTransactions { get; set; }
    public virtual IList<TransactionLink> ParentTransactions { get; set; }

    public Transaction()
    {
        ChildTransactions = new List<TransactionLink>();
        ParentTransactions = new List<TransactionLink>();
    }

    /// <summary>
    /// Use this to add a payment to a charge to pay it off, or to add a refund on a payment to un-pay it
    /// </summary>
    /// <param name="inTransaction"></param>
    /// <param name="inAmount"></param>
    public virtual void AddChildTransaction(Transaction inTransaction, Decimal inAmount)
    {
        TransactionLink link = new TransactionLink()
                                            {
                                                TransactionParent = this,
                                                TransactionChild = inTransaction,
                                                Amount = inAmount
                                            };

        if (!ChildTransactions.Contains(link))
            ChildTransactions.Add(link);

        if (!inTransaction.ParentTransactions.Contains(link))
            inTransaction.ParentTransactions.Add(link);
    }

    /// <summary>
    /// You probably shouldnt be using this.
    /// </summary>
    /// <param name="inTransaction"></param>
    /// <param name="inAmount"></param>
    public virtual void AddParentTransaction(Transaction inTransaction, Decimal inAmount)
    {
        TransactionLink link = new TransactionLink()
                                            {
                                                TransactionChild = this,
                                                TransactionParent = inTransaction,
                                                Amount = inAmount
                                            };

        if (!inTransaction.ChildTransactions.Contains(link))
            inTransaction.ChildTransactions.Add(link);

        if (!ParentTransactions.Contains(link))
            ParentTransactions.Add(link);
    }
}

[Serializable]
public class TransactionLink : Entity
{
    public virtual Int64 TransactionIdParent { get; protected internal set; }
    public virtual Transaction TransactionParent { get; set; }

    public virtual Int64 TransactionIdChild { get; set; }
    public virtual Transaction TransactionChild { get; protected internal set; }

    public virtual DateTime LastModifiedOn { get; set; }
    public virtual Decimal Amount { get; set; }

    #region Override comparison - as this is a composite key we need to custom roll our comparison operators

    #endregion
}

图:

public TransactionMap()
    {
        Id(x => x.TransactionId);

        Map(x => x.AccountId);
        Map(x => x.TransactionTypeId);
        Map(x => x.TransactionStatusId);
        Map(x => x.DateCreated);
        Map(x => x.DateEffective);
        Map(x => x.Amount);
        HasMany(x => x.ParentTransactions).Cascade.None().KeyColumn("TransactionIdParent").LazyLoad();
        HasMany(x => x.ChildTransactions).Cascade.All().KeyColumn("TransactionIdChild").LazyLoad();
    }

public TransactionLinkMap()
    {
        CompositeId()
            .KeyProperty(x => x.TransactionIdParent, "TransactionIdParent")
            .KeyProperty(x => x.TransactionIdChild, "TransactionIdChild");

        References(x => x.TransactionParent).Column("TransactionIdParent").Cascade.SaveUpdate().LazyLoad();
        References(x => x.TransactionChild).Column("TransactionIdChild").Cascade.All().LazyLoad();

        Version(x => x.LastModifiedOn);
        Map(x => x.Amount);
    }

我正在调整问题并从nHibernate获取各种错误 - 有人请告诉我最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

Gah,问题发现 - 在Jonas指出其他设置之后,我认为问题必须是复合键 - 答案是什么?不要使用复合键!

即使设置了Version()属性,nHibernate也会看到所有Transient对象都是相同的,因此当我尝试在一次点击中添加3个事务时,它就搞乱了。

这个问题的解决方案,如果有其他人遇到同样的事情,就是在多对多表中添加一个自动增量键,并将其用作nHibernate中的一个键,这使得nHibernate更容易区分瞬态物体!