问题描述
对于一个学校项目,我应该设计一个上学期项目的系统。我们正在使用UML,创建一个非常简单的用例图(没有< extend> 类型的废话,只是参与者指向系统内部的用例) ,我们从中创建一个类图,然后创建一个关系模型。
For a school project, I'm supposed to design the systems of a previous semester's project. We're using UML, creating an extremely simple use case diagram (no <<extend>> type nonsense, just actors pointing at use cases inside of a system), from which we make a class diagram, then a relational model.
用例和类图尝试:
最后一个图只是我最终认为也许我的用例是问题所在。
The last diagram is just me eventually thinking that maybe my use cases were the issue.
我的应用程序允许人们/餐厅列出他们愿意捐赠的食物。养活无家可归者的慈善机构要求食物。如果有比赛,慈善机构可以创建一条卡车路线来从所有比赛中拾取食物。
My application lets people/restaurants list food they're willing to donate. Charities that feed the homeless request food. If there's a match, the charities can create a truck route to pick up food from all their matches.
我很难弄清楚如何表示匹配项,以及FoodItems的菱形和匹配连接路线是否有意义。我知道继承在关系建模中通常不是一个好主意。这仅仅是到处都有FK的问题吗?我感觉好像在转圈。
I'm having trouble figuring out how to represent matching, as well as if this diamond of the FoodItems and matching connecting to route makes any sense. I know inheritance is generally a bad idea in relational modeling. Is it just a matter of having FKs everywhere? I feel like I'm walking in circles.
推荐答案
这个对象关系映射主题可能非常复杂。但是,查看您的图,这里是一个过分简化的经验法则:
This object relational mapping topic can be quite complex. But looking at your diagram, here an oversimplified rules of thumbs:
- 映射每个类时,不对关系表进行泛化也没有对其进行专门化。当存在时,这些字段将作为主键。
- 将每个具有两个专业化的类映射到一个关系表,该关系表将类的字段及其所有专业化的字段组合在一起。此技术称为。并非总是一个好的解决方案,但可以肯定地适合您的情况。
- 对于每个一对一关系( 1-1-.. * 或 1-0.。 * )在1侧的表中添加1侧的主键(ID)作为外键。对您的聚合也执行此操作。这称为。
- 对于您的n到n链接( 1 .. *-1 .. * ),您需要添加一个关系表,两个链接表的ID(每个都定义为外键,并且都一起作为组合的主键)。这称为。
- 对于第二个 ItemMatch 中的一对一聚合,我认为将两个字段分别与 foodItemID (使用其他名称)。
- 您还需要将由于 0 .. ** $code>而添加的外键定义为可选(可空)关系
- Map each class with no generalization and no specialization to a relational table. When ID fields are present, these will act as primary key.
- Map each of your classes having two specialization, to one relational table that combines the fields of the class and all the fields of its specialization. This technique is called single inheritance table. It's not always a good solution, but in your cas it could definitively fit.
- For each one to n relationship (1 - 1..*, or 1-0..*) add the primary key (ID) on the 1 side as foreign key in the table on the n side. Do this also for the your aggregates. This is called foreign key mapping.
- For your n to n link (1..* - 1..*) you need to add a relational table, with the ID of the two linked tables (each defined as foreign key, and both together as a combined primary key). This is called association table mapping.
- for your one to one aggregation in the second ItemMatch, I think it cwould be sufficient to add two fields corresponding each to a foodItemID (use different names).
- You also need to define as optional (nullable) the foreign keys that you've added as result of a 0..* relation
其他读数:
- 马丁Fowler的出色著作 说明了几种针对该应用程序的映射策略的利弊如果想了解更多,可以继承完整的细节。除了简单的单一继承表,您还将找到更复杂的替代方法,例如,和。
- 网络文章在注释中建议将对象映射到数据模型(谢谢!)
- Martin Fowler's excellent book "Patterns of Enterprise Application Architecture" explains the pros and cons of several mapping strategies for inheritance in full details if want to know more. In addition to the simple single inheritance table, you'll find also more complex alternatives approaches like class table inheritance, concrete table inheritance and inheritance mappers.
- Web article Mapping object to data model suggested by granier in the comments (thanks !)
这篇关于将类图转换为关系模型;继承和用于匹配的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!