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.
分享到:
相关推荐
LOAM ,Lidar Odometry and mapping in real time;一个开源的3d slam建图方法,值得参考
不喜欢NHibernate传统的*.hbm.xml来对数据库进行映射的朋友有福了,可以使用NHibernate.Mapping.Attribute.dll来实现自动映射。 减少不小的工作量。
Table of Contents Chapter One: Object/Relational Persistence in .Net Chapter Two: Hello NHibernate Chapter Three: Writing and Mapping classes Chapter Four: Working with persistent objects...
NHibernateMapping的Mygeneration框架
NHibernate.Mapping-1.1.csgen
Nhibernate拒绝配置文件(NHibernate.Mapping.Attributes的使用)
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, ...
NHibernate实例分享:Northwind Mapping
Mapping Objects to Relational Databases O-R Mapping In Detail.doc
MyGeneration NHibernate 代码生成模板
Overview Integration Builder Mapping – Introduction
NHibernate 3.2配置实例,mapping by code映射
视频采集端hdr优化
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 Mapping代码生成器
NHibernate持久层通用组建,简单易用
Nhibernate各数据库配置模版, Nhibernate Mapping 文档模版
Land Cover Mapping in Limited Labels Scenario A Survey.pdf
Land Cover Mapping in Limited Labels Scenario A Survey.zip
c#编写无SQL程序,ORM自动映射,NHibernate-5.2.4-bin.zip ,里面代码完全,亲测好用,与大家共享