1

今天我在我的领域模型上测试了一些东西,我意识到这种行为不是我所期望的。

我试图隔离创建一个简单Customers-Orders模型的问题。

这是我的映射。

顾客:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="NHVariousTests.Domain.Customer, NHVariousTests" table="Customers">
    <id name="Code" type="System.Guid">
        <column name="CustomerCode" />
        <generator class="guid.comb" />
    </id>
    <version name="Version" type="System.Int32" unsaved-value="0" access="backfield"">
        <column name="Version" not-null="true" />
    </version>
    <property name="Description" type="AnsiString">
          <column name="Description" not-null="false" />
    </property>   
    <set name="Orders" access="field.pascalcase-underscore" cascade="all-delete-orphan" inverse="true" lazy="true">
        <key column="CustomerCode" />
        <one-to-many class="NHVariousTests.Domain.Order, NHVariousTests" />
    </set>
  </class>
</hibernate-mapping>

命令:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="NHVariousTests.Domain.Order, NHVariousTests" table="Orders">
    <id name="Code" type="System.Guid">
        <column name="OrderCode" />
        <generator class="guid.comb" />
    </id>
    <version name="Version" type="System.Int32" unsaved-value="0" access="backfield">
        <column name="Version" not-null="true" />
    </version>
    <property name="Description" type="AnsiString">
        <column name="Description" not-null="false" />
    </property>   
    <many-to-one class="NHVariousTests.Domain.Customer, NHVariousTests" name="Customer">
        <column name="CustomerCode" not-null="true" />
    </many-to-one>
  </class>
</hibernate-mapping>

我的Order课很简单:

public class Order : EntityGuid
{
    public Order()
    {
    }

    public virtual string Description { get; set; }

    public virtual Customer Customer { get; set; }
}

这是我的Customer课:

public class Customer : EntityGuid
{
    public Customer()
    {
        this._Orders = new HashSet<Order>();
    }

    public virtual string Description { get; set; }

    #region Orders

    private readonly ICollection<Order> _Orders = null;

    public virtual ReadOnlyCollection<Order> Orders
    {
        get { return (new List<Order>(_Orders).AsReadOnly()); }
    }

    public virtual bool AddOrder(Order order)
    {
        if ((order != null) && (!this._Orders.Contains(order)))
        {
            order.Customer = this;
            this._Orders.Add(order);
            return (true);
        }
        return (false);
    }

    public virtual bool RemoveOrder(Order order)
    {
        if ((order != null) && (this._Orders.Contains(order)))
        {
            this._Orders.Remove(order);
            order.Customer = null;
            return (true);
        }
        return (false);
    }

    #endregion
}

在第一个示例中,我使用了一个ICollection<Order>创建为HashSet<Order>.

如果我运行此代码:

using (var session = sessionFactory.OpenSession())
{
    using (var tx = session.BeginTransaction())
    {
        var customer = new Domain.Customer() { Description = "ACME Ltd" };

        var order = new Domain.Order() { Description = "Coffee" };
        customer.AddOrder(order);

        order = new Domain.Order() { Description = "Milk" };
        customer.AddOrder(order);

        session.Save(customer);

        tx.Commit();
    }
}

我可以看到INSERT客户的和INSERT订单的两个,但我也有UPDATE客户的。

在此处输入图像描述

如果我改变我的Customer班级切换到Iesi收藏:

public class Customer : EntityGuid
{
    public Customer()
    {
        this._Orders = new Iesi.Collections.Generic.HashedSet<Order>();
    }

    public virtual string Description { get; set; }

    #region Orders

    private readonly ICollection<Order> _Orders = null;

    public virtual ReadOnlyCollection<Order> Orders
    {
        get { return (new List<Order>(_Orders).AsReadOnly()); }
    }

    public virtual bool AddOrder(Order order)
    {
        if ((order != null) && (!this._Orders.Contains(order)))
        {
            order.Customer = this;
            this._Orders.Add(order);
            return (true);
        }
        return (false);
    }

    public virtual bool RemoveOrder(Order order)
    {
        if ((order != null) && (this._Orders.Contains(order)))
        {
            this._Orders.Remove(order);
            order.Customer = null;
            return (true);
        }
        return (false);
    }

    #endregion
}

我得到了预期的行为:

在此处输入图像描述

这是我的EntityGuid课:

public abstract class EntityGuid : EntityWithTypedId<Guid>, IAuditedEntity
{
    public EntityGuid()
    {
        this.CreatedDate = DateTime.Now;
        this.UpdatedDate = DateTime.Now;
        this.CreatedBy = "";
        this.UpdatedBy = "";
    }

    public virtual DateTime CreatedDate { get; set; }
    public virtual string CreatedBy { get; set; }

    public virtual DateTime UpdatedDate { get; set; }
    public virtual string UpdatedBy { get; set; }

    public virtual int Version { get; private set; }

    public virtual bool IsTransient()
    {
        return (EntityWithTypedId<Guid>.Equals(this.Code, default(Guid)));
    }

    public virtual bool IsTransient(EntityWithTypedId<Guid> obj)
    {
        return obj != null && Equals(obj.Code, default(Guid));
    }

}

有没有人可以帮助我尝试了解正在发生的事情?

问候。

如果有人真的有兴趣看看代码。我已经清理了所有东西并使其尽可能简单。可以在这里下载(NH_CollectionProblems)。

我正在使用 NHibernate 3.3.2.4000

4

2 回答 2

1

您期望的行为是错误的,您应该期望在版本化实体上修改集合会更新版本。此行为在用于映射集合的文档中进行了描述,该文档还描述了如何禁用它:

乐观锁(可选 - 默认为 true):更改集合状态的物种会导致拥有实体的版本增加。(对于一对多关联,禁用此设置通常是合理的。)

我确实拉下项目并验证更改此设置会切换正在发布的更新:false = 无更新,true(默认)= 更新。但是,我无法使用 Iesi HashedSet 获得预期的行为。我认为这是一个错误,但我现在没有更多时间来处理它。

于 2013-01-11T13:45:51.433 回答
1

任何一个...

于 2013-01-08T12:12:37.447 回答