NHibernate映射不会填充包

时间:2011-03-10 16:01:54

标签: nhibernate-mapping

<class name="CashThreshold" table="CASH_THRESHOLD_COUNTERS" lazy="true" >
<id name="Id" column="ID" >
  <generator class="assigned" />
</id>
<bag name="ThresholdNominalsList" cascade="all" inverse="true" lazy="false" table="CASH_THRESHOLD_CAS_COUNTERS">
  <key column="CASH_THRESHOLD_ID" />
  <one-to-many class="NominalThreshold" />
</bag>

映射第二张表

<class name="NominalThreshold" table="CASH_THRESHOLD_CAS_COUNTERS" lazy="true" >
<composite-id>
  <key-property name="CashTrasholdId" column="CASH_THRESHOLD_ID" type="long"></key-property>
  <key-property name="Nominal" column="NOMINAL" type="long"></key-property>
</composite-id>

<property name="MinNoteCount" column="MIN_NOTE_COUNT" />
<property name="MaxNoteCount" column="MAX_NOTE_COUNT" />

表类

public class CashThreshold : ICashThreshold
{
    public virtual long Id { set; get; }

    /// !!!!!!! IS ALWAYS AMPTY, but not null !!!!!
    public virtual IList<INominalThreshold> ThresholdNominalsList { set; get; }
}

public class NominalThreshold : INominalThreshold
{
    public virtual long CashTrasholdId { set; get; }
    public virtual long Nominal { set; get; }
    public virtual long MinNoteCount { set; get; }
    public virtual long MaxNoteCount { set; get; }

    public override bool Equals(Object obj)
    {
        var tmp = (INominalThreshold)obj;
        return (tmp.CashTrasholdId == CashTrasholdId && tmp.Nominal == Nominal);
    }

    public override int GetHashCode()
    {
        return (int)CashTrasholdId ^ (int)Nominal;
    }
}

获取 ICashThreshold

列表的功能
        ICriteria selectAll = currentSession.CreateCriteria<ICashThreshold>();

        IList<ICashThreshold> list = selectAll.List<ICashThreshold>();

执行查询没有错误。在sql-client中成功执行了包查询并返回了4个结果,但 IList&lt; INominalThreshold&gt; ThresholdNominalsList 没有元素。

感谢。

1 个答案:

答案 0 :(得分:0)

问题解决了。 NHibernate映射包成功,但列表为空,因为数据库中的数据未提交。我在表中插入了测试数据,但没有提交。当我在sql-client中执行查询时,它执行成功(因为它在会话中,插入了表行),但是hibernate有另一个会话。这就是NHibernate无法看到表数据的原因。