2

NHibernate 遇到一些错误,例如“无法初始化集合”,但似乎已生成正确的 sql 并成功从数据库中检索数据。

这是我的代码和日志,请帮助检查它们。谢谢。

(1) 域实体代码:

[Serializable]
    class ProductionOrderLineItem : IProductionOrderLineItem
    {
        protected IProductionOrder _productionOrder;
        protected int? _lineNum;
        protected IItem _item;
        protected decimal? _baseQty;
        protected decimal? _plannedQty;
        protected decimal? _issuedQty;
        protected string _issueType;
        protected IWarehouse _warehouse;

        public virtual IProductionOrder ProductionOrder
        {
            get { return _productionOrder; }
            private set { _productionOrder = value; }
        }

        public virtual int? LineNum
        {
            get { return _lineNum; }
            private set { _lineNum = value; }
        }

        public virtual IItem Item
        {
            get { return _item; }
            private set { _item = value; }
        }

        public virtual decimal? BaseQty
        {
            get { return _baseQty; }
            private set { _baseQty = value; }
        }

        public virtual decimal? PlannedQty
        {
            get { return _plannedQty; }
            private set { _plannedQty = value; }
        }

        public virtual decimal? IssuedQty
        {
            get { return _issuedQty; }
            private set { _issuedQty = value; }
        }

        public virtual string IssueType
        {
            get { return _issueType; }
            private set { _issueType = value; }
        }

        public virtual IWarehouse Warehouse
        {
            get { return _warehouse; }
            private set { _warehouse = value; }
        }

        public override bool Equals(object obj)
        {
            if (null == obj)
                return false;
            var t = obj as IProductionOrderLineItem;
            if (null == t)
                return false;
            if (ProductionOrder == t.ProductionOrder && LineNum == t.LineNum && Item == t.Item)
                return true;
            return false;
        }

        public override int GetHashCode()
        {
            return string.Format("{0}#{1}#{2}", ProductionOrder.ProductionOrderId, LineNum, Item.ItemCode).GetHashCode();
        }
    }

(2) O/R 映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MyProject.Production.ProductionOrderLineItem, MyProject" table="ProductionOrderLineItems" proxy="MyProject.Production.IProductionOrderLineItem, MyProject">
    <composite-id>
        <key-many-to-one name="ProductionOrder" class="MyProject.Production.ProductionOrder, MyProject" column="ProductionOrderId"/>
        <key-property name="LineNum" type="int" column="LineNum"/>
    </composite-id>
    <many-to-one name="Item" class="MyProject.Inventory.Item, MyProject">
        <column name="ItemCode" sql-type="nvarchar" length="20"/>
    </many-to-one>

(3) NHibernate 错误日志:

2012-11-09 13:30:34,354 [4] DEBUG NHibernate.SQL [(null)] - SELECT lineitems0_.ProductionOrderId as ProductionOrderId1_, lineitems0_.LineNum as LineNum1_, lineitems0_.ProductionOrderId as ProductionOrderId9_0_, lineitems0_.LineNum as LineNum9_0_, lineitems0_.ItemCode as ItemCode9_0_, lineitems0_.BaseQty as BaseQty9_0_, lineitems0_.PlannedQty as PlannedQty9_0_, lineitems0_.IssuedQty as IssuedQty9_0_, lineitems0_.IssueType as IssueType9_0_, lineitems0_.warehouse as warehouse9_0_ FROM ProductionOrderLineItems lineitems0_ WHERE lineitems0_.ProductionOrderId=@p0;@p0 = 1680
2012-11-09 13:30:34,374 [4] DEBUG NHibernate.Event.Default.DefaultLoadEventListener [(null)] - loading entity: [MyProject.Production.ProductionOrder#1680]
2012-11-09 13:30:34,376 [4] DEBUG NHibernate.Event.Default.DefaultLoadEventListener [(null)] - entity found in session cache
2012-11-09 13:30:34,397 [4] DEBUG NHibernate.Driver.NHybridDataReader [(null)] - running NHybridDataReader.Dispose()
2012-11-09 13:30:34,399 [4] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] - Closed IDataReader, open IDataReaders :0
2012-11-09 13:30:34,400 [4] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] - DataReader was closed after 43 ms
2012-11-09 13:30:34,402 [4] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] - Closed IDbCommand, open IDbCommands: 0
2012-11-09 13:30:34,403 [4] DEBUG NHibernate.AdoNet.ConnectionManager [(null)] - aggressively releasing database connection
2012-11-09 13:30:34,404 [4] DEBUG NHibernate.Connection.ConnectionProvider [(null)] - Closing connection
2012-11-09 13:30:34,409 [4] DEBUG NHibernate.Util.ADOExceptionReporter [(null)] - could not initialize a collection: [MyProject.Production.ProductionOrder.LineItems#1680]
4

1 回答 1

1

查看所有内部异常及其消息。80% 的时间你都可以在这里找到解决方案。

如果这不能解决它:

  1. 错误日志没有列出任何实际错误,但也许这就是你的意思?
  2. 异常发生在哪里,你做了什么来触发它?
于 2012-11-09T09:29:55.537 回答