nhibernate单向一对多映射子实体数据记录中父实体ID未同时保存
当执行测试代码后,子实体和父实体均能保存到数据库,但是子实体对应的数据表中记录的父实体id的值全部为null,数据库为mysql,请问这是正常的么,是否需要设置双向映射后父实体ID才会同步保存?1) father entity definition:
class father
{
public virtual string name;
public virtual string id;
public virtual IList<string> children;
}
2) child entity definition:
class child
{
public virtual string id;
public virtual string name;
}
3) mapping file for father
<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) mapping file for child
<class name ="child" table="dbchild">
<id name="id" column ="id">
<generator class ="assigned"/>
</id>
<property name ="name"/>
</class>
5) test codes
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();
}
} NHibernate 数据库 --------------------编程问答-------------------- 没人帮忙看看? --------------------编程问答-------------------- 顶一下,请人看看 --------------------编程问答-------------------- public virtual IList<string> children;???
public virtual IList<Children> children; --------------------编程问答-------------------- Child 类里怎么没有对父类的引用?参考
这里的例子
http://richardneililagan.com/2010/09/mapping-parent-child-relationships-in-nhibernate/
或
http://www.codeproject.com/Articles/472019/Object-Relational-Mapping-ORM-u --------------------编程问答--------------------
不好意思,那个是笔误,应该是ilist<child> --------------------编程问答--------------------
思归大哥,因为我是跟着博客园的教程来学习对nhibernate的使用,问题代码使用的是单向的一对多映射,因为教程未对这个问题现象加以说明,我也不知道是因为单向映射就是这样的结果,还是我给出的这些代码和配置文件有问题导致的?
不过先看看你给的文章
补充:.NET技术 , C#