我有一个针对场景的“最佳实践”问题。
设想:
DB 中的多个实体,例如 Document、BlogPost、Wiki 可以由个人共享。不是为每个实体创建共享表,而是创建单个共享表。问题是,如何将共享表与不同的实体进行映射?
我有三个选项,请告知哪个选项最好,以及是否有更好的选项。
Option1:创建表共享为:
SHARES
id (unique)
entityId (non DB enforced FK to DOCUMENTS, WIKIS, POSTS etc.)
entityType
sharedBy
sharedWith
sharedDate
在这里,entityId 将是 documentId、wikiId、postId 等的外键,entityType 将标识entityId 的类型。
在创建共享到实体映射(例如 share.getDocument() 或 share.getWiki() 等)时,这在 Hibernate 建模中存在问题。
选项2:创建仅保存共享信息的表 Shares,然后创建将共享与实体关联的解析表。
SHARES
id(PK)
sharedBy
sharedWith
sharedDate
shareType (helper field for searches)
SHARES_DOCUMENTS
share_id (unique ID and FK, one to one with SHARES)
document_id (FK to DOCUMENTS)
SHARES_POST
share_id (unique ID and FK, one to one with SHARES)
post_id (FK to POSTS)
more share tables here.
因此,在休眠方面,共享对于每种共享类型都可以是一对一的(例如 share.getDocument()、share.getPost() 和 shareType 将识别哪个关系是“活动的”)
Option 3与选项 1 类似,但创建单独的列而不是实体 ID
SHARES
id (unique ID)
documentId (FK to DOCUMENTS, nullable)
postId (FK to POSTS, nullable)
wikiId (FK to WIKIS, nullable)
sharedBy
sharedWith
sharedDate
sharedType
在这里,每一列都可以映射到各自的实体,但它们可以为空。 SharedType 可以识别哪个关系是“活动的”。
所以,问题是,哪种实践是最好的,无论是数据库方面还是休眠映射(以及最终的查询,性能方面)。
谢谢
M·拉瑟