0

如果有人可以帮助我吗?为什么执行测试代码后,字段fatherid的值为null?1)父实体定义:

class father
{
   public virtual string name;
   public virtual string id;
   public virtual IList<child> children;
}

2)子实体定义:

class child
{
   public virtual string id;
   public virtual string name;
}

3)父亲的映射文件

<class name ="father" table="dbfather">
<id name="id" column ="id">
  <generator class ="assigned"/>
</id>
<property name ="name"/>
<bag name="ichild">
<key column="fatherid" />
<one-to-many class="child" />
</bag>
</class>

4)孩子的映射文件

<class name ="child" table="dbchild">
<id name="id" column ="id">
  <generator class ="assigned"/>
</id>
<property name ="name"/>
</class>

5) 测试代码

var cfg = new NHibernate.Cfg.Configuration().Configure("hibernate.cfg.xml");
        using (ISessionFactory sessionFactory = cfg.BuildSessionFactory())
        {
            ISession session = sessionFactory.OpenSession();
            try
            {
                Child c = new Child();
                c.id= Guid.NewGuid().ToString();
                c.name= "test"; 
                Father f = new Father();
                f.id = Guid.NewGuid().ToString();
                f.name= "ftest";
                f.children.Add(child); 

                session.Save(c);
                session.Save(f);
            }
            finally
            {
                session.Flush();
            }
        }
4

1 回答 1

0
public virtual IList<string> children;

应该

public virtual IList<Child> children;
于 2013-06-11T12:32:21.327 回答