核心数据:我可以添加与父实体的关系吗?
我创建了 Photo
实体,它有一个 Data
属性来存储图像数据。 Photo
是Thumbnail
实体的父级。我还在 Photo
和 Thumbnail
之间添加了一对一的关系,以便 Photo
可以拥有 Thumbnail
。
这似乎有效,但有点令人困惑。
您认为创建另一个具有图像数据属性的名为 Image
的实体并制作 Photo
& 是更好的设计吗? Image
的 Thumbnail
子级?
I made the Photo
entity, which has a Data
attribute to store the image data. Photo
is the parent of the Thumbnail
entity. I also added a one-to-one relationship between Photo
and Thumbnail
so that a Photo
can have a Thumbnail
.
This seems to work but is sort of confusing.
Do you think it's better design to make another entity called Image
that has the image data attribute and make Photo
& Thumbnail
children of Image
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该将实体继承视为进行整洁设计的一种手段,而应该使用它来使多个实体可以处于相同的关系中。
假设您有一个包含图像数据的
Person
实体,但图像的大小或类型不同,但您需要所有图像实体处于相同的关系中。您可以像这样进行设置:因为
GIF
和JPG
实体继承自Image
,因此您可以将它们都放在Person.images 中关系。
在您的情况下,除了类比之外,似乎没有什么特殊原因可以使
Photo
和Thumbnail
继承自一个公共的超级实体。此外,如果您使用 SQLite 存储,则父实体的所有子实体最终都位于同一个表中。对于非常大的数据集,这可能会产生性能问题。因此,这是不为了简洁而使用实体继承的另一个原因。
You shouldn't look at entity inheritance as a means of making a tidy design but instead it should be used such that multiple entities can inhabit the same relationship.
Suppose you had a
Person
entity that had image data but the images where of different sizes or types but you needed all the image entities to be in the same relationship. You would set things up like so:Because the
GIF
andJPG
entities inherit fromImage
you can put both in thePerson.images
relationship.In your case, there appears to be no particular reason, other than analogy to Classes, to make the
Photo
andThumbnail
inherit from a common super entity.Also, if you are using an SQLite store, all the subentities of a parent entity end up in the same table. For very large data sets, this can create performance problems. So, that is another reason not to use entity inheritance just for neatness.