我有一个场景,我正在保存一个对象图,我需要稍后在另一个过程中检索它。数据处理的简单案例。
问题在于,在成功保存对象及其关联集合之后(如输出插入语句所示),对同一对象的后续查询会为其关联集合返回一个空集合。我什至在此之前调用了 Flush() 。奇怪的是,关闭并重新打开应用程序会使其返回填充的集合。
我有这个 Appointment 类的映射
<set name="workhours" inverse="true" cascade="all" lazy="true">
  <key>
    <column name="person" />
  </key>
  <one-to-many class="workhour" />
</set>
这是数据检索的代码
Method1()
{
 DataAccessManager.StartOperation();
        IEnumerable<Entity> q = GetQueryProvider();
        if (total > 0)
        {
            q = q.Skip(startIndex).Take(total);
        }
        totalfound = q.Count();
        IList<Entity> list = q.ToList();
        DataAccessManager.EndOperation();
        return list;
}
protected override IEnumerable<Entity> GetQueryProvider()
    {
        return DataAccessManager.GetQueryProvider<Appointment>();
    }
这是为了节省
        if (this.Patient.Appointments== null)
            this.Patient.Appointments= new Iesi.Collections.Generic.HashedSet<Appointment>();
        this.Patient.Appointments.Add(this);
        if (this.Doctor.Appointments== null)
            this.Doctor.Appointments= new Iesi.Collections.Generic.HashedSet<Appointment>();
        this.Doctor.Appoint.Add(this);
        Entity ent = base.Save();
        return ent;
      //the base call:
      DataAccessManager.StartOperation();
        try
        {
            Entity t = DataAccessManager.Save<Entity>(this);
            DataAccessManager.EndOperation();
            return t;
        }
        catch (NHibernate.HibernateException ex)
        {
            //handling            }
//the Data Access save method
 public T Save<T>(T t) where T : Entity
    {
        try
        {
            CurrentSession.SaveOrUpdate(t);
            return t;
  }
 ///exception handling and stuff
  }
我用 Commit() 和 Disconnect() 结束会话。