zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    TCP/IP学习笔记(3)-IP、ARP、RARP协议
    TCP/IP学习笔记(2)-数据链路层
    tcp/ip学习笔记(1)-基本概念
    实体bean里面不要轻易加transient,反序列回来之后会变成null
    [Maven实战-许晓斌]-[第三章] Mave使用入门二(在IDE中的使用) [第四章] 案例的背景介绍
    [Maven实战-许晓斌]-[第三章] Mave使用入门
    [Maven实战-许晓斌]-[第二章]-2.7-2.8 Mave安装的最优建议和安装小结
    [Maven实战-许晓斌]-[第二章]-2.6 NetBeans上面安装Maven插件
    ifdown
    ifup
  • 原文地址:https://www.cnblogs.com/teddyma/p/198576.html
Copyright © 2011-2022 走看看