What happened behind "AllDeleteOrphan" mapping in NHibernate?
Suppose we have the following mapping for Person and FamilyMember. We expect FamilyMember to be removed from db when we remove it from the person.
[HasMany(typeof(FamilyMember), Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true, Fetch = FetchEnum.Select)]
public virtual IList<FamilyMember> FamilyMembers { get; set; }
Clear the FamilyMember.
using (var t = new TransactionScope(TransactionMode.Inherits))
{
Person p1= GetPerson (1);
p1.FamilyMembers.Clear();
}
This is the sql triggered in sql server.
UPDATE persons SET version = @p0, Notes = @p1, primary_email = @p2, secondary_email = @p3,...
UPDATE family_members SET person_id = null
WHERE person_id = @p0
DELETE FROM family_members WHERE Id = @p0
Noteably, it first set the person_id of family_members to null. So, we have to delcare person_id of family_members to allow nullable. Surely, this will lose the constraint that a family_member must belong to an person.
If we really need to keep the constraint in the db level, we can not use "AllDeleteOrphan" anymore. We may resort to other methods to delete the orphane family_memeber manually, like XXXRepository.remove(familyMember).
http://blogs.microsoft.co.il/blogs/arnona/archive/2011/07/12/understanding-one-to-many-relationships-in-nhibernate.aspx
Cascade operations
In parent-child relationships, when you save, update or
delete the parent, usually you want the children to be saved, updated or
deleted as well. Note that for components, this happens automatically
, but often you want this to be the case even if the children are entities.
all-delete-orphan
Notice that by default, if you remove a child element from its
parent, NHibernate can’t determine whether you intend to delete the
child element, or only break the association, leaving the child entity
without a parent. (Recall the definition of an Entity: it’s has a
meaning by its own). Of course that you can call ISession.Delete(child)
whenever you remove a child from its parent, but that would be very
cumbersome, so NHibernate allows us to specify that we want to delete
the children that were removed from their parent (that’s why they called
Orphans
), by specifying cascade=”all-delete-orphan”
on the <bag
…> element
Allow nulls?
Given a parent/child relationships, if you create a new parent entity
with one or more children, and then try to save this parent entity (by
calling ISession.SaveOrUpdate
for example) NHibernate works as follows:
- It persists the parent entity by executing an INSERT statement
- If cascade
is set to save-update
, all
, or all-delete-orphan
,
NHibernate persists each of the child elements by executing an INSERT
statement for each of the children. However, because the relationship is
not part of the child entity (as it’s defined as a bag
element on the parent), the column that defines the key of the bag doesn’t participate in the INSERT and remains NULL
. If we use inverse=true, then we can set the child reference to parent not nullable.
- It updates the key column of the relationship in the child table
with the ID of the parent. (In other words, it associates the children
with the parent)
Beside the fact that a redundant round-trip to the database is
required in this case, it also requires that the key column would allow
nulls, otherwise the 2nd insert will fail!
In foreign-key and bi-directional relationships this problem does not exist because the key column is part of the child entity.
分享到:
相关推荐
《Lidar Odometry and Mapping in Real-Time》(简称LOAM)是一项基于激光雷达(LiDAR)的实时定位与建图技术,该技术由Ji Zhang和Sanjiv Singh在2014年的Robotics: Science and Systems会议上提出。这项研究主要...
不喜欢NHibernate传统的*.hbm.xml来对数据库进行映射的朋友有福了,可以使用NHibernate.Mapping.Attribute.dll来实现自动映射。 减少不小的工作量。
【标题】"NHibernateMapping.rar" 是一个包含与NHibernate映射相关的资源的压缩文件,很可能是使用MyGeneration工具生成的代码模板。MyGeneration是一款开源的代码生成工具,它允许用户自定义模板来自动化常见的编程...
NHibernateMapping的Mygeneration框架
NHibernate.Mapping-1.1.csgen
A simple utility to generate NHibernate mapping files and corresponding domain classes from existing DB tables. Features: 1. Supports Oracle, SqlServer, PostgreSQL, MySQL, SQLite, Sybase, Ingres, ...
#### 一、O/R Mapping 概念及NHibernate简介 **O/R Mapping**,即对象关系映射(Object/Relational Mapping),是一种编程技术,用于将对象模型与关系型数据库之间建立映射关系。简单来说,它帮助开发者在面向对象的...
在这个“NHibernate 3.2实例”中,我们将深入探讨这个版本的核心特性和如何进行映射配置,特别是通过代码(Mapping by Code)的方式。 在NHibernate 3.2中,映射是定义对象与数据库表之间关系的关键步骤。传统的...
标题中的"C# ORM NHibernate-5.2.4-bin.zip"指的是一个包含C#编程语言中使用的对象关系映射(ORM)框架NHibernate的版本5.2.4的二进制文件包。ORM是一种编程技术,它允许开发人员使用面向对象的语言(如C#)来操作...
当我们谈论"Northwind Mapping"时,我们实际上是在讨论如何使用NHibernate来定义这些数据库表与.NET类之间的映射关系。 1. **安装NHibernate**:在项目中引入NHibernate库,可以通过NuGet包管理器进行安装。同时,...
NHibernate 是一款强大的开源对象关系映射(ORM)框架,广泛应用于.NET开发中,它为.NET程序员提供了在数据库上操作对象的便捷方式。这个“NHibernate持久层通用组建”是针对NHibernate框架的一个扩展,旨在简化...
Optionally, you can describe your mapping metadata with attributes in your source code. NHibernate supports transparent persistence, your object classes don't have to follow a restrictive ...
NHibernate 是一款强大的对象关系映射(ORM)框架,专为.NET环境设计。ORM框架的主要目的是简化数据库操作,将复杂的SQL语句与业务逻辑代码解耦,使得开发人员可以使用面向对象的方式处理数据库操作。NHibernate是...
NHibernate Mapping代码生成器
Land Cover Mapping in Limited Labels Scenario: A Survey Land cover mapping是指通过遥感技术和机器学习方法来监测和分类地球表面的土地利用类型,如森林、耕地、城市等。这种技术在监测全球环境变化和管理...
2. **映射(Mapping)**:Nhibernate使用XML或注解来定义实体类和数据库表之间的映射关系。 3. **会话(Session)**:这是Nhibernate的核心组件,它管理了数据库会话,提供对象的持久化和检索。 4. **会话工厂...
- **Fluent Mapping**:使用 Fluent NHibernate,使用 C# 代码定义映射,更具可读性。 5. **会话管理** - **NHibernate.Session**:它是与数据库交互的主要接口,用于执行 CRUD(创建、读取、更新和删除)操作。 ...
开发者定义实体类的属性,并使用特性(如`NHibernate.Mapping.Attributes`或Fluent NHibernate)来指定它们与数据库列的关系。 3. **映射文件**:这是XML文件,描述了实体类如何映射到数据库表。例如,使用...