ORM中的对象间关联及其默认事务需要 - The Object Associations And The Default Transaction Requirements In ORM
2005-07-23 12:37
537 查看
本文对在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
对于对象的基本关联以及对象关联怎样映射到关系数据库的表结构,请参见我之前的两篇文章:
[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
相关文章推荐
- ORM中的对象间关联及其默认事务需要 - The Object Associations And The Default Transaction Requirements In ORM
- const at the end of function and passing const object in a function call
- Day 5 Designing the System: A Crash Course in Object-Oriented Analysis and Design
- 设置页面的默认焦点控件和默认接受事件控件How Do I Set the DefaultFocus or DefaultButton in a Page Based on a Master Page in ASP.NET 2.0
- OpenStack: What is the difference between Block storage and Object storage?Also, In what way they ar
- The Default Methods And Static Methods In Java Interface
- Executing a batch in a transaction--PHP DATA OBJECT(PDO)事务处理中的批量操作
- Default Bearer and Dedicated Bearer in the LTE
- Using JPA and JAXB Annotations in the Same Object
- Setting the Reply-To Header in an Email using CDONTS.NewMail Object and CDO Message
- Changing the Starts With Filter in a Webtop 6.5 Object Locator into a Contains Filter(将查找对象对话框中的'以...开始'改为'包含')
- PHP中对象的clone和引用的区别(Object Cloning and Passing by Reference in PHP)
- Modify and set the default shell in Solaris10
- 从2009年至2014年脸书(Facebook)的用户行为及其发展 The User Behavior in Facebook and its Development from 2009 until
- c++ what happens when a constructor throws an exception and leaves the object in an inconsistent state?
- Python中使用pickle对内建类型(built in types)进行对象序列化(object serialization and deseirialzation)
- props default 数组(Array)/对象(Object)的默认值应当由一个工厂函数返回
- The DefaultModelBinder in ASP.NET MVC -ASP.NET MVC的默认模型绑定器
- SQL 2005 Tips --事务处理The current transaction cannot be committed and cannot support operations that write to the log file
- Always override Object.equals() and Object.hashCode() in the same time