本文对在ORM中对象间的关联及其映射到关系数据库表时的默认事务需要做一个整理。
对于对象的基本关联以及对象关联怎样映射到关系数据库的表结构,请参见我之前的两篇文章:
[1] UML Model/Relation Db Table Mapping Design For LiteMda - Draft
[2] O/R Mapping中对象关系映射解决方案汇总
To practise my english writing skill for technical atrticles, the following contents will be english only. Say sorry to any body that be annoyed to reading english.
--------
Generalization
There are three main mapping strategies for generalization:
1) Mapping hierarchy to a single table(ref. [2] 1.1)
Needn't transaction.
2) Mapping each concrete class to this own table(ref. [2] 1.2.2)
Needn't Transaction.
3) Mapping each class to its own table(ref. [2] 1.3)
When doing inserting, wannaing to save attributes of a sub class into db, you should not only insert into the table mapped to the sub class but also into the tables mapped to all the parent classes. It is similar when doing updating.
When deleting rows mapped to sub class, you should also deleting those in parent tables.
Association(Including Directed Association)
Association means a class have an attricbute as an reference to another class.(ref. [1]) So when deleting a row, you should set-null all the references in the deleted object's associated classes' mapping tables.
Aggregation
Class B aggregated by class A means class A can have a class B.(ref. [1], [2] 2.1). So when deleting a row in table mapped to B, you should set-null its references in table mapped to A.
Composition
If class A is composed of class B and class C, and B is the essential(which means a 1 - 1 association) part, when deleting b, which is an instance of B, it also means a, which is the instance of A and having b as an essential part(a 1 -1 association), needn't exist. So when deleting b, you should also delete a. In addition, if A is composed of B and C, and the composition association to B is 1 - 0..1 or 1- 0.. *, set-nulling will be enough, or if it is 1 - 1..*, then only when a has'nt even one b, deleting-a is needed when deleted some b.
Self Association
There are several situations:
1) Self Aggregation
Like normal aggregation, you should do some set-nulling when delete a row.
2) Self Composition
It seems there will never be the situation that A is composed of A and some other classes and a's existence depending on at least one A. So set-nulling is enough. But if there really come the situation like this, it is needed to related-rows-deleting too.
In Addition
If class on one side and on even either side of an association is a class in an inheritence hierarchy, the situation will become somedegree more complex. Then, you should treat the class in an inheritence hierarchy as a logic single table, do the related-row deleting or set-nulling on the logic table first, and then map the logic table's operations to the physical tables in the hierarchy.(It is the situation that parent associates its sub class which idior mentioned in the comment of [1])
//The End