【发布时间】:2018-10-01 17:03:48
【问题描述】:
以为我一个人就能搞定,但是……我觉得我浪费了太多时间…… 大家好!我正在为 Room 数据库结构苦苦挣扎——我不确定我的方法是否正确。我想要实现的是能够将服务(子子类别)添加到类别中,但也可能有可选的子类别:
类别 -> 服务
类别 -> 可选子类别 -> 服务
我已经做过的事情(跳过了一些基本的代码部分):
a) 创建实体
@Entity
class Category
int id
String name
@Entity
class Subcategory
int id
String name
int categoryID
@Entity
class Subcategory
int id
String name
int categoryID
int subcategoryID
b) 创建连接表
@Entity
class Join
int id
int categoryID
int subcategoryID
int serviceID
当一个类别包含任何服务的子类别时,这很简单:
id | categoryId | SubcategoryId| ServiceId|
-------------------------------------------
1 | 1 | 1 | 1 |
1 | 1 | 1 | 2 |
但如果没有子类别并且服务仅属于类别:
id | categoryId | SubcategoryId| ServiceId|
-------------------------------------------
1 | 1 | null | 1 |
我将null 设置为 SubcategoryId 并检查 subcategory 是否为空
所以问题是:这种方法在关系数据库设计方面是否正确?
或者我应该: 1. 创建category-subcategory、subcategory-service、category-service 连接表并最终以某种方式将它们连接成一个大连接? 2.还是在服务中添加categoryId和subcategoryId?
非常感谢任何 cmets、提示和技巧! :)
【问题讨论】:
标签: android database-design android-room