ORM中的对象间关联及其默认事务需要 - The Object Associations And The Default Transaction Requirements In ORM

本文对在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值