Databases and Deep Copy

后端 未结 4 494
花落未央
花落未央 2021-01-19 19:32

If I find myself wanting to do a deep copy of an object stored in my relational database, have I necessarily done something fundamentally wrong architecturally? This is a di

4条回答
  •  抹茶落季
    2021-01-19 20:06

    It depends on what your relational tables represent, and which of your entities are grouped together to build a "business object". For example, if you have a relational model of a content management system with a table "Postings" and a table "Text_lines" where every Posting consists of a list of text lines, a valid copy of your business object "Posting" will most likely be a deep copy of a "Postings" entity together with the belonging Text_lines entities.

    On the other hand, having two tables "Department" and "Employees", a correct copy of a department most likely is not a deep copy, (at least, if each employee is associated with exactly one department at one point in time). In a relational database scheme, those kind of differences will often go hand-in-hand with referential integrity checks assigned to the relations. The relation "Postings"->"Text_lines" can be modeled using a foreign key constraint with an "ON DELETE CASCADE" integrity (of course, if your database supports that). The "Department"-> "Employees", however, should not have such an "ON DELETE CASCADE" check on it.

提交回复
热议问题