开发者

instantiating an nhibernate Collection

in my Order Entity i am getting a null ref on orderItems. This occurs when an order is first created. I'd like to be able to add something like

order.OrderItems = new Iesi.Collections.Set<OrderItem>();

the error is "Cannot access protected constructor "Set" here

    public class Order
    {
        public virtual int Id { get; set; }
        public virtual ISet<OrderItem> OrderItems { get; set; }

        public virtual void AddOrderItem(OrderItem item)
        {
            item.Order = this;
            // OrderItems below is null
            OrderItems.Add(item); 
        }

    }


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="EStore.Domain"
                   namespace="EStore.Domain.Model">

  <class name="Order" table="Orders">
    <id name="Id" type="Int32" column="Id" unsaved-value="0">
      <generator class="identity"/>
    </id>

    <property name="UserName" column="UserName"/>
    <set name="OrderItems" table="OrderItems" generic="true" cascade="all-delete-orphan"  inverse="true" >
      <key column="OrderId" not-null="true" />
      <one-to-many class="EStore.Domain.Model.OrderItem, EStore.开发者_运维百科Domain" />
    </set>

  </class>
</hibernate-mapping>


Set OrderItems to a new List in the constructor

public Order() {
 OrderItems = new List<OrderItem>();
}


as A Bunch mentioned above.

"Since Iesi.Collections.Set is an abstract class you will need to use one of the implementations such as Iesi.Collections.HashedSet, Iesi.Collections.HybridSet, Iesi.Collections.ListSet or Iesi.Collections.SortedSet"

I've updated the following method. The key part is "new HashedSet(); "

 public virtual void AddOrderItem(OrderItem item)
    {
        item.Order = this;
        if (item.Order.OrderItems == null)
        {
            item.Order.OrderItems = new HashedSet<OrderItem>(); 
        }
        OrderItems.Add(item);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜