nhibernate单向一对多映射子实体数据记录中父实体ID未同时保存

fromocean 2013-06-10 06:08:49
当执行测试代码后,子实体和父实体均能保存到数据库,但是子实体对应的数据表中记录的父实体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();
}
}
...全文
118 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fromocean 2013-06-12
  • 打赏
  • 举报
回复
引用 4 楼 saucer 的回复:
Child 类里怎么没有对父类的引用?参考 这里的例子 http://richardneililagan.com/2010/09/mapping-parent-child-relationships-in-nhibernate/ 或 http://www.codeproject.com/Articles/472019/Object-Relational-Mapping-ORM-u
思归大哥,因为我是跟着博客园的教程来学习对nhibernate的使用,问题代码使用的是单向的一对多映射,因为教程未对这个问题现象加以说明,我也不知道是因为单向映射就是这样的结果,还是我给出的这些代码和配置文件有问题导致的? 不过先看看你给的文章
fromocean 2013-06-12
  • 打赏
  • 举报
回复
引用 3 楼 saucer 的回复:
public virtual IList<string> children;??? public virtual IList<Children> children;
不好意思,那个是笔误,应该是ilist<child>
saucer 2013-06-12
  • 打赏
  • 举报
回复
Child 类里怎么没有对父类的引用?参考 这里的例子 http://richardneililagan.com/2010/09/mapping-parent-child-relationships-in-nhibernate/ 或 http://www.codeproject.com/Articles/472019/Object-Relational-Mapping-ORM-u
saucer 2013-06-12
  • 打赏
  • 举报
回复
public virtual IList<string> children;??? public virtual IList<Children> children;
fromocean 2013-06-11
  • 打赏
  • 举报
回复
顶一下,请人看看
fromocean 2013-06-10
  • 打赏
  • 举报
回复
没人帮忙看看?
最新整理的NHibernate中文文档 目录 第1章 NHibernate体系结构 1 总览 1 第2章 ISessionFactory配置 2 可编程配置方式 3 获取ISessionFactory 3 用户自行提供ADO.NET连接 3 NHibernate提供ADO.NET连接 3 可选配置属性 4 Logging 6 第3章 持久化类(Persistent Classes) 6 POCO 简单示例 6 实现继承(Inheritance) 7 实现Equals()和GetHashCode() 7 持久化生命周期(Lifecycle)中的回调(Callbacks) 7 合法性检查(Validatable)回调 7 用属性替代 XML 7 第4章 O/R Mapping基础 7 让我们从一个映射的例开始: 8 Schema 9 hibernate-mapping 9 class 9 id 10 联合ID(composite-id) 12 识别器(discriminator) 12 (缺了很多) 12 第5章 集合类(Collections)映射 13 持久化集合类(Persistent Collections) 13 映射集合(Mapping a Collection) 13 值集合和多对多关联(Collections of Values and Many-To-Many Associations) 14 一对多关联(One-To-Many Associations) 14 延迟初始化(延迟加载)(Lazy Initialization) 14 集合排序(Sorted Collections) 14 使用 14 双向关联(Bidirectional Associations) 14 三重关联(Ternary Associations) 15 异类关联(Heterogeneous Associations) 15 集合例 15 第6章 关联映射 15 简介 15 单向关联 15 使用表连接的单向关联 16 双向关联 16 使用表连接的双向关联 16 第7章示例: Parent/Child 16 关于collections 17 双向的一对多关系(Bidirectional one-to-many) 17 级联生命周期(Cascading lifecycle) 18 级联更新(Using cascading update()) 19 结论 19 第8章 NHibernate缓存(NHibernate.Caches) 19 什么是 NHibernate.Caches? 19 如何使用? 20 第9章 使用AttributesNHibernate.Mapping.Attributes 20 如何使用? 20 提示 21 已知的问题和TODOs 22 开发者须知 22 第10章 NHibernate.Tool.hbm2net 23 什么是 NHibernate.Tool.hbm2net? 23 第11章 Nullables 23 什么是 Nullables? 23 如何使用? 23 重点 24
前言 ............................................................................ xi 1. 教程 ......................................................................... 1 1.1. 第一部分 - 第一个 Hibernate 应用程序 ................................. 1 1.1.1. 设置 ............................................................ 1 1.1.2. 第一个 class ................................................... 3 1.1.3. 映射文件 ........................................................ 4 1.1.4. Hibernate 配置 .................................................. 7 1.1.5. 用 Maven 构建 .................................................. 9 1.1.6. 启动和辅助类 .................................................... 9 1.1.7. 加载并存储对象 ................................................. 10 1.2. 第二部分 - 关联映射 ................................................. 13 1.2.1. 映射 Person 类 ................................................ 13 1.2.2. 单向 Set-based 的关联 ......................................... 14 1.2.3. 使关联工作 ..................................................... 15 1.2.4. 值类型的集合 ................................................... 17 1.2.5. 双向关联 ....................................................... 18 1.2.6. 使双向连起来 ................................................... 19 1.3. 第三部分 - EventManager web 应用程序 ................................. 20 1.3.1. 编写基本的 servlet ............................................. 20 1.3.2. 处理与渲染 ..................................................... 21 1.3.3. 部署与测试 ..................................................... 24 1.4. 总结 ................................................................. 25 2. 体系结构(Architecture) ..................................................... 27 2.1. 概况(Overview) ...................................................... 27 2.2. 实例状态 .............................................................. 29 2.3. JMX 整合 ............................................................. 30 2.4. 对 JCA 的支持 ........................................................ 30 2.5. 上下文相关的会话(Contextual Session) ................................ 30 3. 配置 ........................................................................ 33 3.1. 可编程的配置方式 ...................................................... 33 3.2. 获得 SessionFactory .................................................. 34 3.3. JDBC 连接 ............................................................ 34 3.4. 可选的配置属性 ........................................................ 36 3.4.1. SQL 方言 ...................................................... 42 3.4.2. 外连接抓取(Outer Join Fetching) .............................. 43 3.4.3. 二进制流(Binary Streams) ..................................... 43 3.4.4. 二级缓存与查询缓存 ............................................. 43 3.4.5. 查询语言中的替换 ............................................... 43 3.4.6. Hibernate 的统计(statistics)机制 ............................. 43 3.5. 日志 ................................................................. 44 3.6. 实现 NamingStrategy .................................................. 44 3.7. XML 配置文件 ......................................................... 45 3.8. J2EE 应用程序服务器的集成 ............................................. 46 3.8.1. 事务策略配置 ................................................... 46HIBERNATE - Relational Persis... iv 3.8.2. JNDI 绑定的 SessionFactory ..................................... 47 3.8.3. 在 JTA 环境下使用 Current Session context(当前 session 上下文) 管理 .................................................................. 48 3.8.4. JMX 部署 ...................................................... 48 4. 持久化类(Persistent Classes) .............................................. 51 4.1. 一个简单的 POJO 例 ................................................. 51 4.1.1. 实现一个默认的(即无参数的)构造方法(constructor) ............. 52 4.1.2. 提供一个标识属性(identifier property)(可选) ................. 52 4.1.3. 使用非final的类(可选) ........................................ 53 4.1.4. 为持久化字段声明访问器(accessors)和是否可变的标志(mutators) (可选) .............................................................. 53 4.2. 实现继承(Inheritance) ............................................... 53 4.3. 实现 equals() 和 hashCode() 方法: ................................... 54 4.4. 动态模型(Dynamic models) ............................................ 55 4.5. 元组片断映射(Tuplizers) ............................................. 57 4.6. EntityNameResolvers ................................................... 58 5. 对象/关系数据映射基础(Basic O/R Mapping) ................................. 61 5.1. 映射定义(Mapping declaration) ....................................... 61 5.1.1. Doctype ........................................................ 62 5.1.2. Hibernate-mapping .............................................. 63 5.1.3. 类 ............................................................. 64 5.1.4. id ............................................................. 67 5.1.5. 增强的标识符生成器 ............................................. 71 5.1.6. 标识符生成器的优化 ............................................. 72 5.1.7. composite-id ................................................... 72 5.1.8. 鉴别器(discriminator) ........................................ 73 5.1.9. 版本(version)(可选) ........................................ 74 5.1.10. timestamp(可选) .............................................. 75 5.1.11. Property ...................................................... 76 5.1.12. 多对一(many-to-one) ......................................... 78 5.1.13. 一对一 ........................................................ 80 5.1.14. 自然 ID(natural-id) ......................................... 82 5.1.15. 组件(component)和动态组件(dynamic-component) ............... 82 5.1.16. 属性(Properties) ............................................ 83 5.1.17. 类(subclass) .............................................. 84 5.1.18. 连接的类(joined-subclass) ................................. 85 5.1.19. 联合类(union-subclass) .................................... 86 5.1.20. 连接(join) .................................................. 87 5.1.21. Key ........................................................... 88 5.1.22. 字段和规则元素(column and formula elements) ................. 89 5.1.23. 引用(import) ................................................ 90 5.1.24. Any ........................................................... 90 5.2. Hibernate 的类型 ..................................................... 91 5.2.1. 实体(Entities)和值(values) ................................. 91v 5.2.2. 基本值类型 ..................................................... 92 5.2.3. 自定义值类型 ................................................... 93 5.3. 多次映射同一个类 ...................................................... 94 5.4. SQL 中引号包围的标识符 ............................................... 95 5.5. 其他元数据(Metadata) ................................................ 95 5.5.1. 使用 XDoclet 标记 ............................................. 95 5.5.2. 使用 JDK 5.0 的注解(Annotation) .............................. 97 5.6. 数据库生成属性(Generated Properties) ................................ 98 5.7. 字段的读写表达式 ...................................................... 98 5.8. 辅助数据库对象(Auxiliary Database Objects) .......................... 99 6. 集合映射(Collection mappings) ............................................ 101 6.1. 持久化集合类(Persistent collections) ............................... 101 6.2. 集合映射( Collection mappings ) ................................... 102 6.2.1. 集合外键(Collection foreign keys) ........................... 103 6.2.2. 集合元素(Collection elements) ............................... 104 6.2.3. 索引集合类(Indexed collections) ............................. 104 6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) ....................................................... 105 6.2.5. 一对多关联(One-to-many Associations) ........................ 107 6.3. 高级集合映射(Advanced collection mappings) ......................... 108 6.3.1. 有序集合(Sorted collections) ................................ 108 6.3.2. 双向关联(Bidirectional associations) ........................ 109 6.3.3. 双向关联,涉及有序集合类 ...................................... 111 6.3.4. 三重关联(Ternary associations) .............................. 112 6.3.5. Using an <idbag> ............................................. 112 6.4. 集合例(Collection example) ....................................... 113 7. 关联关系映射 ............................................................... 117 7.1. 介绍 ................................................................ 117 7.2. 单向关联(Unidirectional associations) .............................. 117 7.2.1. 多对一(many-to-one) ......................................... 117 7.2.2. 一对一(One-to-one) .......................................... 117 7.2.3. 一对多(one-to-many) ......................................... 118 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) .. 119 7.3.1. 一对多(one-to-many) ......................................... 119 7.3.2. 多对一(many-to-one) ......................................... 120 7.3.3. 一对一(One-to-one) .......................................... 120 7.3.4. 多对多(many-to-many) ........................................ 121 7.4. 双向关联(Bidirectional associations) ............................... 122 7.4.1. 一对多(one to many)/多对一(many to one) .................... 122 7.4.2. 一对一(One-to-one) .......................................... 123 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) ... 124 7.5.1. 一对多(one to many)/多对一(many to one) .................... 124 7.5.2. 一对一(one to one) ......................................... 125 7.5.3. 多对多(many-to-many) ........................................ 126HIBERNATE - Relational Persis... vi 7.6. 更复杂的关联映射 ..................................................... 126 8. 组件(Component)映射 ....................................................... 129 8.1. 依赖对象(Dependent objects) ........................................ 129 8.2. 在集合中出现的依赖对象(Collections of dependent objects) ........... 131 8.3. 组件作为 Map 的索引(Components as Map indices ) .................... 132 8.4. 组件作为联合标识符(Components as composite identifiers) ............ 132 8.5. 动态组件(Dynamic components) ....................................... 134 9. 继承映射(Inheritance Mapping) ............................................ 137 9.1. 三种策略 ............................................................. 137 9.1.1. 每个类分层结构一张表(Table per class hierarchy) ............. 137 9.1.2. 每个类一张表(Table per subclass) .......................... 138 9.1.3. 每个类一张表(Table per subclass),使用辨别标志 (Discriminator) .................................................... 138 9.1.4. 混合使用“每个类分层结构一张表”和“每个类一张表” ........... 139 9.1.5. 每个具体类一张表(Table per concrete class) .................. 140 9.1.6. 每个具体类一张表,使用隐式多态 ................................ 141 9.1.7. 隐式多态和其他继承映射混合使用 ................................ 142 9.2. 限制 ................................................................ 142 10. 与对象共事 ................................................................ 145 10.1. Hibernate 对象状态(object states) ................................. 145 10.2. 使对象持久化 ........................................................ 145 10.3. 装载对象 ............................................................ 146 10.4. 查询 ............................................................... 148 10.4.1. 执行查询 ..................................................... 148 10.4.2. 过滤集合 ..................................................... 152 10.4.3. 条件查询(Criteria queries) ................................. 153 10.4.4. 使用原生 SQL 的查询 ......................................... 153 10.5. 修改持久对象 ........................................................ 153 10.6. 修改脱管(Detached)对象 ............................................ 154 10.7. 自动状态检测 ........................................................ 155 10.8. 删除持久对象 ........................................................ 156 10.9. 在两个不同数据库间复制对象 .......................................... 156 10.10. Session 刷出(flush) .............................................. 157 10.11. 传播性持久化(transitive persistence) ............................. 158 10.12. 使用元数据 ......................................................... 160 11. Read-only entities ........................................................ 161 11.1. Making persistent entities read-only ................................ 162 11.1.1. Entities of immutable classes ................................ 162 11.1.2. Loading persistent entities as read-only ..................... 162 11.1.3. Loading read-only entities from an HQL query/criteria ......... 163 11.1.4. Making a persistent entity read-only ......................... 165 11.2. Read-only affect on property type .................................. 166 11.2.1. Simple properties ............................................ 167 11.2.2. Unidirectional associations .................................. 167

111,112

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧